简体   繁体   English

SQL Server转换问题与datetime

[英]SQL Server conversion issue with datetime

I always use this code for conversion in datetime: 我总是在datetime中使用此代码进行转换:

 DECLARE @a datetime
 SET @a= CONVERT(datetime,'2012-12-28 14:04:43') 
 print @a

But this does not work anymore! 但这不再适用了! I tried even restarting SQL Server, but the problem remains: 我甚至试过重启SQL Server,但问题仍然存在:

转换错误

The error in the image is in Italian. 图像中的错误是意大利语。 In English should be: 英文应该是:

The conversion of a char data type to datetime resulted in a datetime value that is out of range of allowed values​​. 将char数据类型转换为datetime会导致datetime值超出允许值的范围。

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT . SQL Server支持许多格式 - 请参阅CDN和CONVERT上MSDN联机丛书 Most of those formats are dependant on what settings you have - therefore, these settings might work some times - and sometimes not. 大多数这些格式取决于您拥有的设置 - 因此,这些设置可能会有效 - 有时不会。

The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings. 解决此问题的方法是使用SQL Server支持的(略微调整的) ISO-8601日期格式 - 此格式始终有效 - 无论您的SQL Server语言和日期格式设置如何。

The ISO-8601 format is supported by SQL Server comes in two flavors: SQL Server支持ISO-8601格式有两种形式:

  • YYYYMMDD for just dates (no time portion); YYYYMMDD仅适用于日期(无时间部分); note here: no dashes! 请注意: 没有破折号! , that's very important! ,这非常重要! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations! YYYY-MM-DD 不是独立的在SQL Server中的日期格式设置,并不会在所有情况下工作!

or: 要么:

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME . YYYY-MM-DDTHH:MM:SS表示日期和时间 - 请注意:此格式短划线(但可以省略),并且固定T作为DATETIME的日期和时间部分之间的分隔符。

This is valid for SQL Server 2000 and newer. 这适用于SQL Server 2000及更高版本。

If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME !), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server. 如果您使用SQL Server 2008或更高版本以及DATE数据类型(仅DATE - 而不是 DATETIME !),那么您确实也可以使用YYYY-MM-DD格式,这也适用于SQL Server中的任何设置。

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. 不要问我为什么整个主题如此棘手而且有些混乱 - 这就是它的方式。 But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server. 但是使用YYYYMMDD格式,您可以适用于任何版本的SQL Server以及SQL Server中的任何语言和日期格式设置。

So in your concrete case - use this: 所以在你的具体案例中 - 使用这个:

DECLARE @a datetime
SET @a= CONVERT(datetime, '2012-12-28T14:04:43') 
print @a

and this should work on any SQL Server installation, with any language and date format settings. 这适用于任何 SQL Server安装,具有任何语言和日期格式设置。

If you run your original code for US English - it will work just fine: 如果您运行美国英语的原始代码 - 它将正常工作:

SET LANGUAGE English

DECLARE @a datetime
SET @a= CONVERT(datetime, '2012-12-28 14:04:43') 
print @a

Dec 28 2012  2:04PM

but if you use Italian (or German, or British, or French) as your language, it will fail because the format without the T in the middle of the date/time string is NOT language-independent and not "safe" : 但如果您使用意大利语(或德国,或英国,或法语)为您的语言,它会失败,因为 没有格式T的日期/时间字符串的中间是不能独立于语言的,而不是“安全”:

SET LANGUAGE Italian

DECLARE @a datetime
SET @a= CONVERT(datetime, '2012-12-28 14:04:43') 
print @a

Msg 242, Level 16, State 3, Line 4 Msg 242,Level 16,State 3,Line 4
La conversione di un tipo di dati varchar in datetime ha generato un valore non compreso nell'intervallo dei valori consentiti. La conversione di un tipo di dati varchar in datetime ha generato un valore non compreso nell'intervallo dei valori consentiti。

You are trying to convert a string to a datetime. 您正在尝试将字符串转换为日期时间。 Problem is with the date part. 问题在于日期部分。 Best way is to get the date string into ISO format (yyyymmdd) and then convert. 最好的方法是将日期字符串转换为ISO format (yyyymmdd)然后转换。 Try this; 试试这个;

DECLARE @a datetime
SET @a= CONVERT(datetime,replace('2012-12-28 14:04:43', '-','')) 
print @a

My guess is that the default date time format changed on your computer. 我的猜测是您的计算机上更改了默认的日期时间格式。 Add the conversion specification back in: 重新添加转换规范:

SET @a= CONVERT(datetime,'2012-12-28 14:04:43', 121) 

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

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