我在一个varchar(50)
列中存储一个日期,其值类似于 thie:
1/01/2018
我想将这些转换为日期时间值,例如: 2018-01-22 00:00:00.0000000
我的 SQL 就像;
select
[Site],
CONVERT(VARCHAR(50), CAST([InvDay] AS DATETIME), 101) as Date,
from tableA;
但我得到了;
将 varchar 数据类型转换为 datetime 数据类型导致值超出范围。
我也试过这样但同样的错误;
CONVERT(datetime, [InvDay]) as Date,
我怎样才能使这项工作?
查看无法转换的值:
select invday
from tableA
where try_cast(invday as date) is null and invday is not null;
也不清楚您的格式是 mm/dd/yyyy 还是 dd/mm/yyyy。您可以使用以下格式指定格式convert()
:
-- mm/dd/yyyy
select invday
from tableA
where try_convert(date, invday, 101) is null and
invday is not null;
-- dd/mm/yyyy
select invday
from tableA
where try_convert(date, invday, 103) is null and
invday is not null;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句