How can I convert UNIX timestamp (bigint) to DateTime in SQL Server?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
If anyone getting "Arithmetic overflow error converting expression to data type int" due to unix timestamp is in bigint (instead of int), you can use this:
SELECT DATEADD(S, CONVERT(int,LEFT(1462924862735870900, 10)), '1970-01-01') FROM TABLE
Replace the hardcoded timestamp for your actual column with unix time stamp
Source: MSSQL bigint Unix Timestamp to Datetime with milliseconds
This will do it:
Instead of !precision! use: ss,ms or mcs according to the precision of the timestamp. Bigint is capable to hold microsecond precision.
Test this:
http://www.w3resource.com/mysql/date-and-time-functions/mysql-from_unixtime-function.php
Better? This function converts unixtime in milliseconds to datetime. It's lost milliseconds, but still very useful for filtering.
try:
This is building off the work Daniel Little did for this question, but taking into account daylight savings time (works for dates 01-01 1902 and greater due to int limit on dateadd function):
We first need to create a table that will store the date ranges for daylight savings time (source: History of time in the United States):
Now we create a function for each American timezone. This is assuming the unix time is in milliseconds. If it is in seconds, remove the /1000 from the code:
Pacific
Eastern
Central
Mountain
Hawaii
Arizona
Alaska