Solved

MS SQL unix timestamp to mm/dd/yyyy conversion

Posted on 2011-09-20
9
494 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

896 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

18 Experts available now in Live!

Get 1:1 Help Now