如何将 '01/01/0001' 转换为 datetime2
How to convert '01/01/0001' to datetime2
如果值为空,我试图显示来自 sql 的“01/01/0001”。但是当我将值转换为 datetime2 时,它说:
Msg 517, Level 16, State 3, Line 18
Adding a value to a 'datetime2' column caused an overflow.
这是我试过的:
Declare @NewDate as DateTime2
SELECT @Tem = Ab.Date_out
FROM Timesheet_header Ab
WHERE (Ab.Employee_id = 3424)
AND (Ab.Work_date BETWEEN ('2017-08-31') AND ('2017-08-31'))
print @Tem
if(@Tem IS NULL)
Begin
set @NewDate = CONVERT(VARCHAR, DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2)),100)
--some select clause here
End
else
Begin
--some select clause here
End
print @Tem
print @NewDate
这是你的问题:
DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2))
Datetime2
最小值为 0001-01-01
,但您尝试创建一个 5 天前的值。
来自 MSDN:
Date range 0001-01-01 through 9999-12-31
此外,您的 @NewDate
变量是 datetime2
类型,但您尝试用 varchar(100)
-
填充它
set @NewDate = CONVERT(VARCHAR, DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2)),100)
这毫无意义。如果有的话,你应该做这样简单的事情:
SET @NewDate = '0001-01-01';
如果值为空,我试图显示来自 sql 的“01/01/0001”。但是当我将值转换为 datetime2 时,它说:
Msg 517, Level 16, State 3, Line 18 Adding a value to a 'datetime2' column caused an overflow.
这是我试过的:
Declare @NewDate as DateTime2
SELECT @Tem = Ab.Date_out
FROM Timesheet_header Ab
WHERE (Ab.Employee_id = 3424)
AND (Ab.Work_date BETWEEN ('2017-08-31') AND ('2017-08-31'))
print @Tem
if(@Tem IS NULL)
Begin
set @NewDate = CONVERT(VARCHAR, DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2)),100)
--some select clause here
End
else
Begin
--some select clause here
End
print @Tem
print @NewDate
这是你的问题:
DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2))
Datetime2
最小值为 0001-01-01
,但您尝试创建一个 5 天前的值。
来自 MSDN:
Date range 0001-01-01 through 9999-12-31
此外,您的 @NewDate
变量是 datetime2
类型,但您尝试用 varchar(100)
-
set @NewDate = CONVERT(VARCHAR, DATEADD(DAY,-5,CAST('0001-01-01' AS DATETIME2)),100)
这毫无意义。如果有的话,你应该做这样简单的事情:
SET @NewDate = '0001-01-01';