SQL 服务器代理中的错误:"the conversation of a varchar data type to a datetime data type resulted in an out-of-range."”
Error in SQL Server Agent: "the conversation of a varchar data type to a datetime data type resulted in an out-of-range.""
我创建了一个在 SQL Server Management Studio 中运行的小脚本,然后我使用相同的脚本创建了一个 SQL 服务器代理作业;但是在我 运行 它之后,我得到一个错误:
The conversation of a varchar data type to a datetime data type resulted in an out-of-range
我通过将日期格式更改为 ISO 8601 解决了这个问题,但我没有发布我的第一个脚本在 SSMS 上的工作方式,而不是在 SQL Server Agent 中的工作方式。
第一个脚本:
declare @teste datetime
set @teste = '31/12/2099 00:00:00'
select @teste
修复错误:
declare @teste datetime
set @teste = '20991231 00:00:00'
select @teste
这就是为什么在使用日期(时间)数据类型时使用明确的格式如此重要的原因之一。 仅格式,在SQL服务器中,无论语言如何,和数据类型都是yyyyMMdd
和yyyy-MM-ddThh:mm:ss
.
对于日期 '31/12/2099 00:00:00'
和您的登录使用的语言,SQL 服务器似乎将该值解释为 2099 年第 31 个月的第 12 天。有一年不到 31 个月,因此出现错误。 (DB<>Fiddle). It's worth noting that date
(and the other "new" datetime data types) behave differently and also can unambiguously understand the format yyyy-MM-dd
; notice in this DB<>fiddle datetime
值的差异仅仅是由于语言设置。
如您所见,解决方案是使用明确的格式。因此,当您使用日期和时间时,我建议使用字符串 '2099-12-31T00:00:00'
.
我创建了一个在 SQL Server Management Studio 中运行的小脚本,然后我使用相同的脚本创建了一个 SQL 服务器代理作业;但是在我 运行 它之后,我得到一个错误:
The conversation of a varchar data type to a datetime data type resulted in an out-of-range
我通过将日期格式更改为 ISO 8601 解决了这个问题,但我没有发布我的第一个脚本在 SSMS 上的工作方式,而不是在 SQL Server Agent 中的工作方式。
第一个脚本:
declare @teste datetime
set @teste = '31/12/2099 00:00:00'
select @teste
修复错误:
declare @teste datetime
set @teste = '20991231 00:00:00'
select @teste
这就是为什么在使用日期(时间)数据类型时使用明确的格式如此重要的原因之一。 仅格式,在SQL服务器中,无论语言如何,和数据类型都是yyyyMMdd
和yyyy-MM-ddThh:mm:ss
.
对于日期 '31/12/2099 00:00:00'
和您的登录使用的语言,SQL 服务器似乎将该值解释为 2099 年第 31 个月的第 12 天。有一年不到 31 个月,因此出现错误。 (DB<>Fiddle). It's worth noting that date
(and the other "new" datetime data types) behave differently and also can unambiguously understand the format yyyy-MM-dd
; notice in this DB<>fiddle datetime
值的差异仅仅是由于语言设置。
如您所见,解决方案是使用明确的格式。因此,当您使用日期和时间时,我建议使用字符串 '2099-12-31T00:00:00'
.