[英]SQL Server : error conversion of a varchar data type
I'm trying to run a SQL query however I'm getting an error 我正在尝试运行SQL查询但是我收到了错误
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. 将varchar数据类型转换为日期时间数据类型会导致超出范围的值。
Code: 码:
SELECT
ClientId,
DatePassed,
Lender
FROM
dbo.tbl_Profile_Mortgage
WHERE
DatePassed = '2011-04-28 00:00:00.000'
I have converted the data type as so 我已经转换了数据类型
SELECT
ClientId,
Convert (Date, DatePassed)
Lender
FROM
dbo.tbl_Profile_Mortgage
WHERE
DatePassed = '2011-04-28 00:00:00.000'
This however is still giving me the same error 然而,这仍然给我同样的错误
Column DatePassed
is of type datetime null
列DatePassed
的类型为datetime null
Any suggestions would be appreciated 任何建议,将不胜感激
Conceptually, you think the where
clause is performed before the convert()
. 从概念上讲,您认为where
子句是在convert()
之前执行的。 But that is not necessarily how the query is run. 但这不一定是查询的运行方式。 The only SQL statement that guarantees order of execution is case
(and then not all the time). 保证执行顺序的唯一SQL语句是case
(然后不是所有时间)。
You can try this: 你可以试试这个:
SELECT ClientId,
(case when isdate(DatePassed) = 1 then Convert (Date, DatePassed) end),
Lender
FROM dbo.tbl_Profile_Mortgage
WHERE DatePassed = '2011-04-28 00:00:00.000';
EDIT: 编辑:
Given that DatePassed
is already in a date time format, then your error is highly unusual. 鉴于DatePassed
已经采用日期时间格式,那么您的错误非常不寻常。 This is not a problem from the conversion of this column, because no varchar
is involved. 这不是转换此列的问题,因为不涉及varchar
。
The only thing I can think of is the constant. 我唯一能想到的就是常数。 Perhaps you have a strange setting for default dates and it is trying to get the 4th day of the 28th month. 也许你有一个奇怪的默认日期设置,它试图获得第28个月的第4天。 If so, you can readily fix this by putting in an explicit conversion with a format: 如果是这样,您可以通过使用以下格式进行显式转换来轻松解决此问题:
SELECT ClientId,
Convert(Date, DatePassed),
Lender
FROM dbo.tbl_Profile_Mortgage
WHERE DatePassed = convert(date, '2011-04-28 00:00:00.000', 121);
Aaron Bertrand has a rather extended discussion of this issue in his blog . Aaron Bertrand在他的博客中对此问题进行了相当广泛的讨论。 By the way, even though I recognize the problem that he mentions with the YYYY-MM-DD format, I still use that by default. 顺便说一下,即使我认识到他用YYYY-MM-DD格式提到的问题,我仍然默认使用它。 I consider it a bug that Microsoft has chosen not to recognize this ISO standard format (but just because I think it is a bug doesn't necessarily convince anyone else). 我认为这是一个微软选择不识别这种ISO标准格式的错误(但仅仅因为我认为这是一个错误并不一定说服其他人)。
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 dependent on what settings you have - therefore, these settings might work some times - and sometimes not. 大多数这些格式取决于您拥有的设置 - 因此,这些设置可能会有效 - 有时不会。 I guess in your case, you're using a language setting that just plain doesn't work with that string literal you're using. 我想在你的情况下,你正在使用一种语言设置,只是简单的不适用于你正在使用的字符串文字。
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中的任何语言和日期格式设置。
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)
,则使用DATETIME2(n)
。 You should try to start phasing out the DATETIME
datatype if ever possible. 您应尝试尽可能开始逐步淘汰DATETIME
数据类型。
In your concrete case - use this string in your WHERE
clause: 在具体案例中 - 在WHERE
子句中使用此字符串:
WHERE DatePassed = '2011-04-28T00:00:00.000';
( notice the literal T
between the date and time portions ) ( 注意日期和时间部分之间的字面值T
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.