Groups | Blog | Home
all groups > sql server programming > august 2007 >

sql server programming : How to convert Excel date seriel number to SQL Datetime?


Matt
8/1/2007 10:22:01 PM
for example, 1/1/2007 is 39083 in Excel.
How to convert the number 39083 to SQL Server datetime type?

declare @datetime datetime
shiju
8/1/2007 10:44:57 PM
[quoted text, click to view]

I don't know how the excel seriel works. I am assuming a serial for a
day.

declare @sr int
set @sr = 39084
select cast('20070101' as DATETIME) + (@sr - 39083)

oj
8/2/2007 1:17:17 AM
Excel uses 1900-Jan-0 as its begin date and Sql uses 1900-Jan-1. So, to
calculate 39083 from 1900-Jan-0, you can do this.

select dateadd(day,-1,39083)

or

select cast('18991231' as datetime) + 39083



--
-oj



[quoted text, click to view]

Bob
8/2/2007 8:28:02 AM
Hmm,

I could only get that to work with -2,

eg SELECT DATEADD( day, -2, 39083 )

not -1. Don't know why!

[quoted text, click to view]
Hugo Kornelis
8/3/2007 10:01:41 PM
[quoted text, click to view]

Hi Bob,

But I do!

A very early version of Excel had an error in the calculations for leap
years: it mistakenly assumed that 1900 was one, whereas in reality, it
wasn't. They could not fix this in later versions, because that would
break compatibility. (That's why you now have the option to choose for a
date representation that starts in 1904 instead <g>).

This means that date values in Excel of 1 through 59 can be converted to
date by adding that number of days to 1899-12-31, date values of 61 and
above by adding it to 1899-12-30, and the number 60 does not represent a
valid date at all.

--
Hugo Kornelis, SQL Server MVP
AddThis Social Bookmark Button