1

In Excel, when I convert these 'General' numbers to dates, which is what they really are, I get different numbers than when I CONVERT() them in SQL Server. The data is coming from an import.

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.

How can I get the real dates as in Excel with a query in SQL Server? As mentioned, I already used CONVERT(datetime, [General]), but then I get the outcomes as in the column SQL Server conversion to datetime.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PRIME
  • 73
  • 1
  • 3
  • 10
  • What is the `convert()` code you are using in SQL Server? Also where is the data coming from? A table? An import? – ollie May 31 '17 at 19:56
  • Excel/Windows and SQL Server have different Day-Zeroes. Off by two days. IIRC, this was an original mistake in Windows V1 (I suspect it was actually two different mistakes combined). – RBarryYoung May 31 '17 at 20:16

1 Answers1

2

To convert an Excel value to a date

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

Demonstration

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

Returns

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
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66