简体   繁体   English

将varchar日期类型转换为datetime错误SQL

[英]Conversion of a varchar date type to a datetime Error SQL

I'm working with the following SQL Query and I'm getting the following error: Msg 我正在使用以下SQL查询,但出现以下错误: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. 242,级别16,状态3,行1将varchar数据类型转换为datetime数据类型导致值超出范围。

My SQL query looks like this: 我的SQL查询如下所示:

SELECT        customer_del_hist.customer_number, MAX(customer_del_hist.serve_location) AS serve_location, MAX(customer_del_hist.product_number) AS product_number, 
                         MAX(customer_del_hist.del_date) AS del_date, MAX(order_rte_bal.warehouse) AS warehouse, MAX(order_rte_bal.route_number) AS route_number, 
                         MAX(order_rte_bal.route_day) AS route_day
FROM            customer_del_hist INNER JOIN
                         inv_master ON customer_del_hist.product_number = inv_master.product_number INNER JOIN
                         order_header ON customer_del_hist.order_number = order_header.order_number INNER JOIN
                         order_rte_bal ON order_header.warehouse_number = order_rte_bal.warehouse AND order_header.route_number = order_rte_bal.route_number AND
                         order_header.route_day = order_rte_bal.route_day AND order_header.delivery_date = order_rte_bal.route_date
WHERE        (customer_del_hist.del_date BETWEEN '5/01/2015' AND '7/31/2015') AND (inv_master.inventory_category IN ('02', '03', '60', '74')) AND 
                         (customer_del_hist.customer_number NOT IN
                             (SELECT        h2.customer_number
                               FROM            customer_del_hist AS h2 INNER JOIN
                                                         inv_master AS i ON h2.product_number = i.product_number
                               WHERE        (h2.del_date BETWEEN '6/01/2014' AND '4/31/2015') AND (i.inventory_category IN ('02', '03', '60', '74'))))
GROUP BY customer_del_hist.customer_number

I've looked at the date in the necessary table and it displays similar to the following: 2014-05-21 00:00:00.000. 我已经在必要的表格中查看了日期,其显示类似于以下内容:2014-05-21 00:00:00.000。 Any thoughts on a work around or a fix? 对变通或修复有任何想法吗?

问题实际上是四月只有30天,而不是31天。

The behavior of CAST and CONVERT with datetime character strings is governed by SET DATEFORMAT. 日期时间字符串的CAST和CONVERT行为由SET DATEFORMAT控制。 If your date format is dmy (day/month/year), your literal '7/31/2015' may be getting interpreted as {Month: 31, Day: 7, Year: 2015}, which is not a valid date. 如果您的日期格式为dmy(日/月/年),则文字'7/31/2015'可能会被解释为{有效月份:{Month:31,Day:7,Year:2015}}。

See here: SET DATEFORMAT (Transact-SQL) 参见此处: SET DATEFORMAT(Transact-SQL)

One thing that will help you in these cases is to always use ISO 8601 format for these literals. 在这些情况下可以帮助您的一件事是始终对这些文字使用ISO 8601格式。 This will bypass the interpretation behavior of CAST and CONVERT. 这将绕过CAST和CONVERT的解释行为。

For your case, you could rewrite it as: 对于您的情况,可以将其重写为:

customer_del_hist.del_date BETWEEN '2015-05-01' AND '2015-07-31'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM