2013-10-03 100 views
1

我們剛剛從2005年升級到SQL Server 2012.雖然我是新手,但這種簡單的事情不可能如此困難。我曾經可以根據日期和日期和時間從表格中提取數據。因爲它現在我有:在SQL Server 2012中按日期搜索

Select * from receipts_table where receipt_cancel_date = '2013-09-20' 

在我們升級之前,這將工作正常。我怎麼能運行這個,並實際得到所需的結果,因爲我知道有取消日期爲2013-09-20的收據。

感謝名單

+0

你期待的是落在的 '2013年9月20日' 的日期的所有記錄?無論時間部分? – Khan

+0

預期結果是什麼? – Khan

回答

1

如果您傳遞字符串日期參數,最好的格式是ISO(yyyymmdd)格式。否則,即使你的字符串在某些服務器上工作,根據服務器的文化,它可能不能在另一個服務器中工作。 ISO format is culture independent

通過將其轉換爲DATE(如果DATETIME)作爲比較目的,從receipt_cancel_date列中刪除時間部分。

試試這個:

Select * from receipts_table 
where convert(date, receipt_cancel_date) = convert(date,'20130920') 

或者使用120風格與格式:

Select * from receipts_table 
where convert(date, receipt_cancel_date) = convert(date,'2013-09-20',120) 
+1

它工作Thankx。 –