Convert number (based on a fixed date value) to SQL Date

I have data that was extracted from an old btrieve database.  I did not extract the data myself or have access to the original data.  I initially received the data in a tab delimited flat text file format which was easily imported into tables onto SQL 2005.

That's when I noticed that all the date fields are compromised of values such as 2429079.  At first I thought this was bad data due to a problem with the conversion.  However after further inspection I am quite convinced they were stored this way as the numbers appear to be sequential based on days.

I was able to match dates to some of these numbers based on known values.  So for example
June 30th, 1938 = 2429079
June 2nd,  1943 = 2430877

If you subtract the numbers and the dates you get the same value if you account for one extra day due to a leap year.  So at least I know I'm not dealing with any time values but rather just days.  Using on of the numbers mentioned above it seems the numbers are days counted up from a fixed date in the pass.  Somewhere around 4700 BC (weird), it's hard to be sure the exact date since the numbers aren't dividing exactly by 365 (I'm sure leap years may have something to do with it).

SO HERE IS THE QUESTION:
Assuming I can find a base point in the past either by figuring out when the days started counting up or by finding the actual date of the smallest number in the dataset... Is there a way to then convert those numbers into a date?
LVL 3
radpatAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:
My approach:
SELECT DATEDIFF(DAY, 0, '1938-06-30') ==> gets 14059 which is difference in days from known number above to 1900-01-01 (0).

Take 2429079 - 14059 = 2415020.

Now using that you can mathematically get date by adding days less that number from 0 or just cast.
DECLARE @dateNum INT
SET @dateNum = 2430877
SELECT DATEADD(DAY, @dateNum - 2415020, 0)
SELECT CAST(@dateNum - 2415020 AS DATETIME)

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Seems like 2415020 is your baseline to date 0 in SQL server.
DECLARE @dateNum INT
SET @dateNum = 2429079
SELECT DATEADD(DAY, @dateNum - 2415020, 0)

Open in new window

0
 
pcelbaCommented:
SQL offers DATEADD() function:
SELECT DATEADD(day, 1798, '1938.06.30')
will result to June 2nd,  1943

So you may calculate almost all dates from your numbers by formula

DATEADD(day, YourNumber-YourConstant, 'YYYY.MM.DD')

YourConstant can be calculated from 1.1.1800 which should be 2378498

I've obtained this number using Visual FoxPro which has functions SYS(10) and SYS(11) - the FoxPro base of calculations is one day moved against your values but I hope the number  2378498 is calculated correctly (one day difference is included already).
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
pcelbaCommented:
Oops, the correction should be opposite:

select DATEADD(day, YourNumber-2378496, '1800.01.01')

But the solution from mwvisa1 works even for dates less than 1.1.1900

0
 
radpatAuthor Commented:
Thanks for the responses.  mwvisa1, I love your solution this is exactly what I needed.  Thanks!  pcelba, thanks for the response as well.  I didn't get a chance to try your solution but it does demonstrate how to use any date as a baseline.
0
 
Kevin CrossChief Technology OfficerCommented:
Definitely.  I chose the approach I did as CAST uses '1900-01-01' as its baseline, so using that gives you ability to use direct conversions.  Glad that helped.

Regards,
kevin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.