将 dd/mm/yyyy 转换为 SQL 服务器中的日期
Convert dd/mm/yyyy to date in SQL Server
我快要将字符串类型的列转换为日期了。
列名称为 StartDate
,其中包含字符串日期格式 dd/mm/yyyy
。字段类型为varchar(3000)
.
我尝试了以下方法:
CONVERT(datetime, StartDate, 103)
CAST(CONVERT(VARCHAR(10), StartDate, 110) AS DATE)
CONVERT(DATE, RIGHT(StartDate, 4) + '-' + SUBSTRING(StartDate, 4, 2) + '-' + LEFT(StartDate, 2), 126)
和其他类似的组合。
我不断收到 "out of range" 和 "conversion failed" 错误消息。
有没有人有创造性的解决方案?
我怀疑你有一些虚假数据。例如
Select try_convert(date, '15/07/2014', 103)
Returns
2014-07-15
如果是2012+,我建议你
Select *
From YourTable
Where try_convert(date, StartDate, 103) is null
这将确定您的问题区域
我快要将字符串类型的列转换为日期了。
列名称为 StartDate
,其中包含字符串日期格式 dd/mm/yyyy
。字段类型为varchar(3000)
.
我尝试了以下方法:
CONVERT(datetime, StartDate, 103)
CAST(CONVERT(VARCHAR(10), StartDate, 110) AS DATE)
CONVERT(DATE, RIGHT(StartDate, 4) + '-' + SUBSTRING(StartDate, 4, 2) + '-' + LEFT(StartDate, 2), 126)
和其他类似的组合。
我不断收到 "out of range" 和 "conversion failed" 错误消息。
有没有人有创造性的解决方案?
我怀疑你有一些虚假数据。例如
Select try_convert(date, '15/07/2014', 103)
Returns
2014-07-15
如果是2012+,我建议你
Select *
From YourTable
Where try_convert(date, StartDate, 103) is null
这将确定您的问题区域