首页 > 解决方案 > 从字符串转换日期和/或时间转换失败

问题描述

declare @orderwhere varchar(5000)
declare @Pdate DateTime

set @orderwhere = 'Product.ProductID = 1 And Product.ProductDate=' + @Pdate

exec('select Product.ProductID
      from Product
      where ' + @orderwhere)

我收到这些错误:

消息 241,级别 16,状态 1,第 4 行
从字符串转换日期和/或时间时转换失败。

标签: sql-server

解决方案


编辑:

注意:此答案是指原始问题,其中包含一行DECLARE @Pdate DATETIME = '1001-01-01'和错误消息“从字符串转换日期和/或时间时转换失败”。在里面。


从手册中datetime

日期范围[:] 1753 年 1 月 1 日至 9999 年 12 月 31 日

所以你的一年肯定是来早了datetime

您可以尝试使用datetime2范围从 0001-01-01 到 9999-12-31 的 a ,也可以尝试使用范围date相同的 a datetime2(关于日期部分,当然不是时间部分)。


推荐阅读