我正在尝试声明一个值为 1001-01-01 00:00:00.000
的日期时间变量我尝试了以下方法,但没有成功
declare @d1 datetime = '1001-01-01';
declare @d2 datetime = 10010101;
declare @d3 datetime = '1001-01-01 00:00:00';
declare @d4 datetime = cast ('1001-01-01' as datetime)
我收到以下错误
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type datetime.
Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
是否可以声明一个可以保存值1001-01-01 00:00:00.000的datetime
变量?
最佳答案
DateTime
数据类型的最小有效日期是 1753 年 1 月 1 日。
尝试DATETIME2
,使用:
DECLARE @d4 DATETIME2 = '1001-01-01'
DATETIME2
的最小有效日期为 0001-01-01
关于sql-server - SQL 声明日期时间 - 1001-01-01,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15528553/