我有一份报告停止在生产环境中工作,直到几个星期前它工作正常。它使用相同的代码在dev中工作正常。这两种环境中的SQL Server 2012日期转换在PROD上失败,在DEV中正常工作
select SUM(oi.quantity_shipped) as [Quantity Shipped],
oi.itemDescription as [Item Description],
oi.itemLanguage
from order_items as oi
inner join orderX as x on oi.ID = x.id
where convert(date, x.shipDate) between '1/1/2016' and '1/1/2017'
group by oi.itemDescription,
oi.itemLanguage
order by [Item Description]
督促代码返回错误:
Msg 241, Level 16, State 1, Line 2 Conversion failed when converting date and/or time from character string.
注意,x.shipDate可以是零。任何想法是什么导致这个?
我的数据库中shipDate的格式是varchar,当它包含一个值时,它的格式如下:2017-01-25 – user2055729
在x.shipdate字段中有一个错误的值。如果可以,请将该列的数据类型更改为datetime – SQLMason
您还应该使用ANSI标准日期格式的字符串。 YYYY-MM-DD。这是唯一的字符串格式,无论dateformat如何都将始终工作。 –