Solved

MS SQL unix timestamp to mm/dd/yyyy conversion

Posted on 2011-09-20
9
506 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 30

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 30

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 30

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
 

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

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.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

808 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