2012-02-22 38 views
6

MSDN清楚地表示:「向'datetime'列添加值導致溢出。」

The date argument cannot be incremented to a value outside the range of its data type. In the following statements, the number value that is added to the date value exceeds the range of the date data type. The following error message is returned: "Adding a value to a 'datetime' column caused overflow."

和示例:

SELECT DATEADD(year,2147483647, '2006-07-31'); 
SELECT DATEADD(year,-2147483647, '2006-07-31'); 

這將導致錯誤:

"Adding a value to a 'datetime' column caused overflow."

這似乎是正確。但是,爲什麼我得到執行這條SQL語句相同的錯誤:

SELECT DATEDIFF(YY,'1013-12-12',DATEADD(YY,-300,getdate())) 

更加具體,只有:

SELECT DATEADD(YY,-300,getdate()) 
+2

最小'datetime'是1753 [因爲那是英國採用公曆之後的一年](http://stackoverflow.com/questions/3310569/what-is-the-significance-of-1-1- 1753-in-sql-server/3310588)你在什麼版本的SQL Server上? – 2012-02-22 19:33:42

+1

@Joro - Well'SELECT DATEADD(YY,-300,cast(getdate()as datetime2))'然後就可以爲你工作。 – 2012-02-22 21:08:12

+0

@MartinSmith是的,你是對的。我檢查過這個,'datetime2'和'datetimeoffset'格式的支持日期從1,0001年1月到9999年12月31日。SQL語句到現在爲止是否意味着getdate()函數的默認類型是'datetime'據說'datetime'和'smalldatetime'折舊。 – gotqn 2012-02-26 11:52:32

回答

9

First google result for 'sql datetime range'。 1753年1月1日,這是你的下限。

對問題的評論添加了this trivia關於此下限的來源。

+0

不錯。謝謝你的回答,但這是一種傷心。例如,如果有人在今年之前在其數據庫中存儲歷史信息,他將無法使用某些內置函數...... – gotqn 2012-02-22 21:02:56

1

如果你使用字段進行DateTime轉換,在轉換中使用case語句來檢查字段是否大於1或1000000,那麼你不應該再有這個問題了。