我在csv文件中有14-DEC-16 06.23.37.000日期。
如何将14-DEC-16 06.23.37.000转换为ssis中的datetime并插入到sql表中。
答案 0 :(得分:0)
据说你正在处理数据,你知道在2000 - 2099之外没有日期,并且你知道你总是会得到与ENG月份缩写相同的字符串格式,你可以使用以下转换:
DECLARE @DateString nvarchar(50) = N'14-DEC-16 06.23.37.000';
WITH cte AS(
SELECT
CASE
WHEN CHARINDEX('-DEC-', @DateString) > 0 THEN REPLACE(@DateString, '-DEC-', '.12.20')
WHEN CHARINDEX('-NOV-', @DateString) > 0 THEN REPLACE(@DateString, '-NOV-', '.11.20')
WHEN CHARINDEX('-OCT-', @DateString) > 0 THEN REPLACE(@DateString, '-OCT-', '.10.20')
WHEN CHARINDEX('-SEP-', @DateString) > 0 THEN REPLACE(@DateString, '-SEP-', '.09.20')
WHEN CHARINDEX('-AUG-', @DateString) > 0 THEN REPLACE(@DateString, '-AUG-', '.08.20')
WHEN CHARINDEX('-JUL-', @DateString) > 0 THEN REPLACE(@DateString, '-JUL-', '.07.20')
WHEN CHARINDEX('-JUN-', @DateString) > 0 THEN REPLACE(@DateString, '-JUN-', '.06.20')
WHEN CHARINDEX('-MAY-', @DateString) > 0 THEN REPLACE(@DateString, '-MAY-', '.05.20')
WHEN CHARINDEX('-APR-', @DateString) > 0 THEN REPLACE(@DateString, '-APR-', '.04.20')
WHEN CHARINDEX('-MAR-', @DateString) > 0 THEN REPLACE(@DateString, '-MAR-', '.03.20')
WHEN CHARINDEX('-FEB-', @DateString) > 0 THEN REPLACE(@DateString, '-FEB-', '.02.20')
WHEN CHARINDEX('-JAN-', @DateString) > 0 THEN REPLACE(@DateString, '-JAN-', '.01.20')
END AS DateString
)
SELECT DateString,
CONVERT(datetime, RIGHT(LEFT(DateString, 10),4)+'-'+SUBSTRING(LEFT(DateString, 10),4,2)+'-'+LEFT(LEFT(DateString, 10),2) +
REPLACE(LEFT(RIGHT(DateString,13),9),'.',':') + RIGHT(DateString, 4)) AS MyDate
FROM cte
我定义了@DateString,但在这里你也可以使用列别名。