[英]Conversion error when converting 'nvarchar' to 'datetime' in sql server
I have this code : 我有这个代码:
DECLARE @StartDate nvarchar
SET @StartDate='22/10/2014'
SELECT CAST (@StartDate as datetime)
And it gives me this error: 它给了我这个错误:
Conversion failed when converting date and/or time from character string.
从字符串转换日期和/或时间时转换失败。
Can anyone suggest a better way/at least no error way to do this. 任何人都可以提出一种更好的方法(至少没有错误的方法)。
There are many formats supported by SQL Server - see the MSDN Books Online on CAST
and CONVERT
. SQL Server支持多种格式-请参阅
CAST
和CONVERT
上的MSDN联机丛书 。 Most of those formats are dependent 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语言和dateformat设置如何。
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! 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
用于日期和时间-请注意:此格式带有破折号(但可以省略),并且在DATETIME
的日期和时间部分之间使用固定的T
作为分隔符。 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中的任何语言和日期格式设置。
The recommendation for SQL Server 2008 and newer is to use DATE
if you only need the date portion, and DATETIME2(n)
when you need both date and time. 对于SQL Server 2008及更高版本,建议仅在需要日期部分的情况下使用
DATE
,在需要日期和时间的情况下使用DATETIME2(n)
。 You should try to start phasing out the DATETIME
datatype if ever possible 如果可能,您应该尝试逐步淘汰
DATETIME
数据类型
To convert the string which represents to date in format dd/mm/yyyy
you can use this: 要转换以
dd/mm/yyyy
格式表示日期的字符串,可以使用以下命令:
DECLARE @StartDate nvarchar(10)
SET @StartDate='22/10/2014'
SELECT CONVERT(datetime, @StartDate, 103)
Declaring varchar
means setting the length to 1. 声明
varchar
意味着将长度设置为1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.