2017-05-31 102 views
1

在Excel中,當我將這些'常規'數字轉換爲日期時,這就是他們真正的日期,我得到的數字與我在SQL Server中的CONVERT()不同。數據來自導入。在SQL Server中錯誤地轉換爲日期時間

General  Excel conversion to date  SQL Server conversion to datetime 
37621  31-12-2002      2003-01-02 00:00:00.000 
39082  31-12-2006      2007-01-02 00:00:00.000 
39447  31-12-2007      2008-01-02 00:00:00.000 
etc. 

我怎樣才能得到真正的日期在Excel與SQL Server中的查詢?如前所述,我已經使用了CONVERT(datetime, [General]),但是後來我得到了SQL Server轉換爲datetime的結果。

+0

什麼是'轉換()'代碼使用的是在SQL Server中?數據來自哪裏?一張桌子?進口? – ollie

+0

Excel/Windows和SQL Server有不同的日子。關兩天。 IIRC,這是Windows V1中的一個原始錯誤(我懷疑它實際上是兩個不同的錯誤組合)。 – RBarryYoung

回答

2

要將Excel值轉換爲日期

Select DateAdd(DAY,General,'1899-12-30') 
From YourTable 

示範

Declare @YourTable Table ([General] int,[Excel conversion to date] varchar(50)) 
Insert Into @YourTable Values 
(37621,'31-12-2002') 
,(39082,'31-12-2006') 
,(39447,'31-12-2007') 

Select * 
     ,DateAdd(DAY,[General],'1899-12-30') 
from @YourTable 

返回

General Excel conversion to date (No column name) 
37621 31-12-2002     2002-12-31 00:00:00.000 
39082 31-12-2006     2006-12-31 00:00:00.000 
39447 31-12-2007     2007-12-31 00:00:00.000 
+0

約翰,我感謝你的回答。但是,當我使用上面的SELECT語句時,它似乎不適用於我...我只需要用我的真實列名稱替換'General'對吧? – PRIME

+0

@PRIME正確。這是爲了演示如何通過DateAdd()將Excel日期值轉換爲SQL Server日期() –

+0

@JohnCappelletti爲什麼1899-12-30? –

相關問題