简体   繁体   English

在SQL Server中将'nvarchar'转换为'datetime'时发生转换错误

[英]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支持多种格式-请参阅CASTCONVERTMSDN联机丛书 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! 注意: 没有破折号! , 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用于日期和时间-请注意:此格式带有破折号(但可以省略),并且在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.

相关问题 sql错误:转换nvarchar时转换失败 - sql error: Conversion failed when converting the nvarchar 将 nvarchar 值“ ”转换为数据类型 int 时,SQL Server 数据库错误转换失败 - SQL Server Database Error Conversion failed when converting the nvarchar value ' ' to data type int SQL Server:将数据类型nvarchar转换为datetime时出错 - SQL Server : error converting data type nvarchar to datetime 将数据类型 nvarchar 转换为日期时间 SQL Server 时出错 - Error converting data type nvarchar to datetime SQL Server SQL Server算术溢出错误-将nvarchar转换为datetime - SQL Server Arithmetic Overflow Error - converting nvarchar to datetime SQL错误:转换nvarchar值时转换失败 - SQL error: conversion failed when converting nvarchar value 在SQL Server中将nvarchar(mm / yyyy)转换为DateTIme - Converting nvarchar (mm/yyyy) to DateTIme in Sql Server SQL Server 2000 将 nvarchar 转换为 datetime 时出现问题 - SQL Server 2000 trouble converting nvarchar to datetime SQL Server。 允许多个值引发错误将nvarchar值…转换为数据类型int时转换失败 - SQL Server. Allow multiple values throwing error Conversion failed when converting the nvarchar value … to data type int 将nvarchar转换为日期-将转换字符串转换为datetime错误 - Converting nvarchar to date - getting conversion string to datetime error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM