2011-05-24 131 views
0

在一個表內有一個包含數據類型datetime的列,我需要將此列中的數據與當前的日期和時間進行比較。SQL Server 2000在將nvarchar轉換爲datetime時遇到問題

我試圖在該領域的轉換,但我在列收到

Syntax error converting datetime from character string 

值不幸的是有三種不同的格式(傳統的垃圾)

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

我的代碼我使用的是如下:

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

有人可以幫助診斷問題請

+1

數據是否將'null'表示爲字符串,或者是記錄實際上是否爲NULL? – JNK 2011-05-24 17:04:56

+0

它實際上是NULL – mattgcon 2011-05-24 17:17:58

回答

2

這兩種日期格式轉換得很好。默認的convert算法做了一個相當體面的工作,即靈活地處理它給出的內容。和一個null字符串,正如我前面評論, will always convert to a null datetime value: per the standard, any expression that involving null yields null`。

我懷疑你有數據問題。最有可能的垃圾字符,如嵌入的CR,LF或CR + LF(換行符)。 HT(製表符)字符也似乎打破convert()。您(或您的DBA)可能需要執行數據清理以擺脫垃圾字符。或者你需要解決這個問題並編寫一個醜陋的表達式來修復運行時的錯誤數據。

像這樣的查詢應該找出問題的數據:

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 

您可能需要加載一個臨時表中的結果,然後,從該

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

,以確定到底是什麼假的字符是。

+0

大聲笑這就是問題,我現在是「DBA」。過去常常運行這個操作的老人不知道他在做什麼(到處都是冗餘和空列)我正在跟着學習 – mattgcon 2011-05-24 17:52:18

+0

好吧我實際上是通過該查詢獲取27行數據。被退回的物品都很好看。他們是有效的日期或有效的日期和時間 – mattgcon 2011-05-24 18:10:00

+0

我也傾倒了一個臨時表的結果,第三列給我的東西像「0x4400650063002E002000310035002C0020003200300030003800」 – mattgcon 2011-05-24 18:13:39

0

你可以轉換爲較新版本的SQL Server嗎?如果是這樣,這個問題會自行解決。如果不是,你必須從等式中排除空值,因爲它們是這裏的問題。

我認爲這不重要,如果您的前1名選擇空或不,因爲我認爲評估失敗,因爲列可以爲空。我在SQL Server 2008 R2中做了一個快速測試,它工作正常。

+0

我們正在轉換,但不是一段時間。這需要在此之前完成,因爲我們正在轉換的日期是未知的。我也嘗試過使用NOT ApplicationDueDate IS NULL的附加子句,但仍然收到錯誤 – mattgcon 2011-05-24 17:20:02

+0

哦,我把1放在那裏只是爲了使查詢變得簡單,它與我實際返回的內容無關 – mattgcon 2011-05-24 17:20:42

+0

@matt - based在他的回答中,只有在列接受NULL時纔有意義,而不是實際上是否有數據。 – JNK 2011-05-24 17:24:23

相關問題