Solved

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

Posted on 2009-05-20
6
465 Views
Last Modified: 2013-12-25
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
Comment
Question by:radpat
  • 3
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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
 
LVL 41

Expert Comment

by:pcelba
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 41

Expert Comment

by:pcelba
Comment Utility
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
 
LVL 3

Author Comment

by:radpat
Comment Utility
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 59

Expert Comment

by:Kevin Cross
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now