修复dbtimestamp转换错误

时间:2019-01-07 12:42:23

标签: sql sql-server ssis

我正在使用siss通过数据转换转换将dd-mm-yyyy varchar输入转换为dbtimestamp字段。我的错误是,转换会产生yyyy-mm-dd,其中mm是输入的dd,而dd是mm。因此,如果我有一个输入04-01-2019 00:00:000它会产生2019-04-01 00:00:000。

我的解决方案是先使用子字符串将输入转换为标准iso格式YYYY-MM_dd,然后再转换为datetime数据类型。我的问题是我该如何纠正现有记录(将dd移至mm并将mm移至dd)?可能又是子字符串了?

1 个答案:

答案 0 :(得分:2)

Assuming your table only has incorrectly implicitly cast values (like '04-01-2019' (dd-MM-yyyy) to 20190401) and none which haven't been, you could use CONVERT and some style codes:

SELECT D,
       CONVERT(date,CONVERT(varchar(10),V.D,101),103)
FROM (VALUES(CONVERT(date,'20190401'))) V(D);

As an UPDATE statement, that would be:

UPDATE YourTable
SET YourDateColumn = CONVERT(date,CONVERT(varchar(10),YourDateColumn ,101),103);

This converts the date back into a varchar with the format MM/dd/yyyy, and converts it back to a date but treats the value as dd/MM/yyyy (thus switching the day and month).