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.
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. And a null
string, as I commented earlier, will always convert to a
nulldatetime value: per the standard, any expression that involving
nullyields
null`.
I suspect you've got a data problem. Most likely junk characters like embedded CR, LF or CR+LF (line breaks). HT (tab) characters also seems to break convert()
. You (or your DBAs) probably need to do a data cleanup to get rid of the junk characters. 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? 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. I did a quick test in SQL Server 2008 R2 and it works fine.
精彩评论