简体   繁体   English

SQL Server 2000 将 nvarchar 转换为 datetime 时出现问题

[英]SQL Server 2000 trouble converting nvarchar to datetime

Within a table there is a column with datatype datetime and I need to compare the data within this column with the current date and time.在表中有一个数据类型为 datetime 的列,我需要将此列中的数据与当前日期和时间进行比较。

I am attempting a convert on the field but I receive我正在尝试在现场进行转换,但我收到了

Syntax error converting datetime from character string

The values in the column unfortunately have three different formats (legacy junk)不幸的是,列中的值具有三种不同的格式(旧垃圾)

November 28, 2005 -or-
5/1/2011 12:00:00 AM -or-
null

My code I am using is as follows:我正在使用的代码如下:

SELECT 1 from webprograms where convert(datetime, ApplicationDueDate) < getdate()

Can someone help diagnose the problem please有人可以帮助诊断问题吗

Both those date formats convert just fine.这两种日期格式都可以很好地转换。 The default convert algorithm does a pretty decent job of being flexible about what it's given.默认的convert算法在其给出的内容方面做得相当不错。 And a null string, as I commented earlier , will always convert to a null datetime value: per the standard, any expression that involving null yields null`.正如我之前评论的那样, null字符串, will always convert to a null datetime value: per the standard, any expression that involving都会yields null`。

I suspect you've got a data problem.我怀疑你有数据问题。 Most likely junk characters like embedded CR, LF or CR+LF (line breaks).最有可能的垃圾字符,如嵌入式 CR、LF 或 CR+LF(换行符)。 HT (tab) characters also seems to break convert() . HT(制表符)字符似乎也打破了convert() You (or your DBAs) probably need to do a data cleanup to get rid of the junk characters.您(或您的 DBA)可能需要进行数据清理以消除垃圾字符。 Or you need to work around the problem and write an ugly expression to fix the bad data at runtime.或者你需要解决这个问题并编写一个丑陋的表达式来修复运行时的坏数据。

A query like this should identify the problem data:像这样的查询应该识别问题数据:

select myCruftyDateTimeColumn,count(*)
from foo
where myCruftyDateTimeColumn is not null
  -- m/d/yyyy hh:mm:ss AM format
  and myCruftyDateTimeColumn not like '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [AP]M'
  and myCruftyDateTimeColumn not like '[0-9][0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [AP]M'
  and myCruftyDateTimeColumn not like '[0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [AP]M'
  -- month d, yyyy alternatives, 2 digit days
  and myCruftyDateTimeColumn not like 'January [0-9][0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'February [0-9][0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'March [0-9][0-9], [0-9][0-9][0-9][0-9]'
  and ...
  and myCruftyDateTimeColumn not like 'October [0-9][0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'November [0-9][0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'December [0-9][0-9], [0-9][0-9][0-9][0-9]'
  -- month d, yyyy alternatives, 2 digit days
  and myCruftyDateTimeColumn not like 'January [0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'February [0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'March [0-9], [0-9][0-9][0-9][0-9]'
  and ...
  and myCruftyDateTimeColumn not like 'October [0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'November [0-9], [0-9][0-9][0-9][0-9]'
  and myCruftyDateTimeColumn not like 'December [0-9], [0-9][0-9][0-9][0-9]'
group by myCruftyDateTimeColumn
order by 1

You might want to load a temp table with the results and then, from that您可能想要加载一个带有结果的临时表,然后从中

select *,convert(varbinary,myCruftyDateTimeColumn)
from #bad_data

to identify exactly what the bogus characters are.以准确识别虚假字符是什么。

Can you convert to a newer version of SQL Server?您可以转换为更新版本的 SQL 服务器吗? If so, the problem will cure itself.如果是这样,问题就会自行解决。 If not, you have to exclude the nulls from the equation, as they are the issue here.如果不是,您必须从等式中排除空值,因为它们是这里的问题。

I don't think it matters if your top 1 is selecting a null or not as I think the evaluation fails as the column is nullable.我认为您的前 1 名是否选择 null 并不重要,因为我认为评估失败,因为该列可以为空。 I did a quick test in SQL Server 2008 R2 and it works fine.我在 SQL Server 2008 R2 中进行了快速测试,它工作正常。

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

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