Solved

MS SQL unix timestamp to mm/dd/yyyy conversion

Posted on 2011-09-20
9
484 Views
Last Modified: 2012-06-27
I have a field in a database that contains unix timestamps.  I recently discovered that it would be handy to have actual dates in the database for troubleshooting purposes.  I only need mm/dd/yyyy, nothing more detailed.  I added a column for the date and have been running a clumsy lookup/update query using php.  I'm having php do the conversion from unix to actual date.  Unfortunately, it's taking forever.

Is there some way within the contructs of SQL to read the unix timestamp from a column and write the corresponding mm/dd/yyyy date format to another column?
0
Comment
Question by:shtaffa
  • 3
  • 3
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Rich Weissler
ID: 36569080
Borrowing heavily from another source...

DATEADD(second, @timestamp, {d '1970-01-01}) where @timestamp is the value for your unix timestamp?
0
 
LVL 29

Expert Comment

by:Rich Weissler
ID: 36569109
And as I test in sql 2005... it looks like there is a quote missing, so:
DATEADD(second, <unixtimestampvalue>, {d '1970-01-01'})
0
 

Author Comment

by:shtaffa
ID: 36569133
Being only moderately fluent in the language of SQL, would it be possible to get a complete example of how to do what I'm trying to accomplish?
0
 
LVL 29

Accepted Solution

by:
Rich Weissler earned 350 total points
ID: 36569184
You have a table that already have a column with some sort of integer value for the UnixTimeStamp.  You've already created a column as a datetime, which I'll call ConvertedDate.

test this first, and backup your database... but you should be able to:

UPDATE <YOURTABLENAME>
  SET <ConvertedDate> = DATEADD(second, <UnixTimeStamp>, {d '1970-01-01'})
0
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

 

Author Comment

by:shtaffa
ID: 36569261
The query you supplied worked really quick.  However, is there a way to format the converted date?  For an example, it added "Oct 1 2011" to the date column.  Is it possible to have it formatted as "10/01/2011"?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36569307
SQL does NOT format the stored DATETIME object. What you are seeing is the result of your default or session language. You can set your language to English and it will default to MM/DD/YYYY display. A DATETIME is a DATETIME, though. And just to elaborate, a UNIX timestamp represents the number of seconds since the UNIX epoch which happens to be 1970-01-01. There really is no reason to use the ODBC canonical form {d '1970-01-01'}.

DATEADD(ss, {unix timestamp}, '19700101')

Works just fine by the way.
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 150 total points
ID: 36569330
If you don't want to change the language, but need to retrieve the date in a specific format then use:

CONVERT(CHAR(10), {your date column}, 101)

Where 101 is the US English format code for MM/DD/YYYY. As suggested by the syntax, you are converting to character data, so keep that in mind when using this technique. It should be done at the point of presentation/display. If any date (chronological) ordering or needs still exist for the date column, I would avoid converting it from a date if that makes sense.
0
 

Author Closing Comment

by:shtaffa
ID: 36569520
Thank you gentlemen.

I ended up using the following query:
UPDATE <MyTable>
  SET <ConvertedDate = CONVERT(CHAR(10), DATEADD(second, <UnixTimeStamp>, {d '1970-01-01'}), 101)
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36569573
Ah, your column is actually character data. For the reasons I said, this is not a best practice, but if consistent the conversion back is trivial. CONVERT(DATETIME, ConvertedDate, 101)

Anyway, best regards and happy coding,

Kevin
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

762 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

22 Experts available now in Live!

Get 1:1 Help Now