[英]Problem with converting NVARCHAR column to DATE datatype
I have a nvarchar
column AddDate
and I tried to use alter column
to convert it to date
, but I get this error我有一个nvarchar
列AddDate
并且我尝试使用alter column
将其转换为date
,但我收到此错误
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.将 nvarchar 数据类型转换为 datetime 数据类型导致值超出范围。
My data looks like this我的数据看起来像这样
AddDate
---------------------
10/2/2019 1:06:00 AM
10/28/2019 3:53:00 PM
Mar 6 2020 6:17PM
Feb 20 2020 12:08PM
10/4/2019
10/4/2019
Not sure a possible way to convert them to dd-mm-yyyy or dd/mm/yyyy不确定将它们转换为 dd-mm-yyyy 或 dd/mm/yyyy 的可能方法
try_convert()
will work with the sample data. try_convert()
将使用示例数据。
try_convert()
will return a NULL value if the conversion fails.如果转换失败, try_convert()
将返回 NULL 值。
Example例子
Declare @YourTable Table ([AddDate] varchar(50)) Insert Into @YourTable Values
('10/2/2019 1:06:00 AM')
,('10/28/2019 3:53:00 PM')
,('Mar 6 2020')
,('Feb 20 2020 12:08PM')
,('10/4/2019')
,('10/4/2019')
Select *
,AsDate = try_convert(date,AddDate)
,AsString = format(try_convert(date,AddDate),'dd-MM-yyyy')
From @YourTable
Returns退货
AddDate AsDate AsString
10/2/2019 1:06:00 AM 2019-10-02 02-10-2019
10/28/2019 3:53:00 PM 2019-10-28 28-10-2019
Mar 6 2020 2020-03-06 06-03-2020
Feb 20 2020 12:08PM 2020-02-20 20-02-2020
10/4/2019 2019-10-04 04-10-2019
10/4/2019 2019-10-04 04-10-2019
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.