創建一個臨時表,其中有一個由數據填充的YourDateString
列,然後是一個實際的日期時間列YourDate
,您將在不同的步驟中更新該列。像這樣:
DECLARE @YourTable table(YourDateString varchar(11), YourDate datetime)
--simulate bulk load
INSERT INTO @YourTable VALUES ('01-JAN-1976',null)
INSERT INTO @YourTable VALUES ('02-FEB-1980',null)
INSERT INTO @YourTable VALUES ('05-MAR-1991',null)
INSERT INTO @YourTable VALUES ('12-APR-1997',null)
INSERT INTO @YourTable VALUES ('22-MAY-1990',null)
INSERT INTO @YourTable VALUES ('18-JUN-1971',null)
INSERT INTO @YourTable VALUES ('17-JUL-1988',null)
INSERT INTO @YourTable VALUES ('19-AUG-1993',null)
INSERT INTO @YourTable VALUES ('19-SEP-1970',null)
INSERT INTO @YourTable VALUES ('04-OCT-1982',null)
INSERT INTO @YourTable VALUES ('08-NOV-1970',null)
INSERT INTO @YourTable VALUES ('18-DEC-1983',null)
--convert to proper datetime
UPDATE @YourTable
SET YourDate= CONVERT(datetime,YourDateString)
--show data
select * from @YourTable
OUTPUT:
YourDateString YourDate
-------------- -----------------------
01-JAN-1976 1976-01-01 00:00:00.000
02-FEB-1980 1980-02-02 00:00:00.000
05-MAR-1991 1991-03-05 00:00:00.000
12-APR-1997 1997-04-12 00:00:00.000
22-MAY-1990 1990-05-22 00:00:00.000
18-JUN-1971 1971-06-18 00:00:00.000
17-JUL-1988 1988-07-17 00:00:00.000
19-AUG-1993 1993-08-19 00:00:00.000
19-SEP-1970 1970-09-19 00:00:00.000
04-OCT-1982 1982-10-04 00:00:00.000
08-NOV-1970 1970-11-08 00:00:00.000
18-DEC-1983 1983-12-18 00:00:00.000
(12 row(s) affected)
那些日期無效,因爲沒有日期'0'。你是如何期待一旦導入日期? – RedFilter 2010-04-13 14:16:17
你可能應該提供一些更多的信息。什麼SQL服務器,什麼樣的操作系統。你有什麼可用的工具 - sed,awk,...? – txwikinger 2010-04-13 14:16:40
我在想他只是以0作爲佔位符來顯示日期來演示格式,而不是實際的數據。 – JohnFx 2010-04-13 14:17:41