Go Premium for a chance to win a PS4. Enter to Win

x
Solved

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

Posted on 2009-05-20
Medium Priority
481 Views
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?
0
• 3
• 2

LVL 60

Expert Comment

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

LVL 60

Accepted Solution

Kevin Cross earned 2000 total points
ID: 24434259
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)
``````
0

LVL 43

Expert Comment

ID: 24434321
will result to June 2nd,  1943

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

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

LVL 43

Expert Comment

ID: 24434416
Oops, the correction should be opposite:

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

0

LVL 3

Author Comment

ID: 24435552
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

LVL 60

Expert Comment

ID: 24435850
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
###### Suggested Courses
Course of the Month13 days, 11 hours left to enroll