SQL: Convert GMT in seconds to DateTime - sql

I have a table with two columns of GMT time in seconds, and offset in minutes like this:
SELECT TOP 1 StartTime, OffSet FROM MyTable;
1247242537 -420 -- as example
What SQL function I can use to convert it to a datetime in yyyy-MM-dd hh:mm:ss as TimeStamp? The SQL I am using is SQL Server 2005.

This should do it, assuming you Epoch date is 1/1/1970 (usually for GMT seconds, but you will need to confirm)
select dateAdd(ss,1247242537+(-420*60),'1/1/1970')
== > 2009-07-10 09:15:37.000
For you code, use
select DATEADD(ss,StartTime+(Offset*60),'1/1/1970') as TheTime FROM myTable
Be sure you find some test cases to make sure the Epoch date is as expected. Basically, the GMT time is number of seconds past 1/1/1970 12:00am, so DateAdd handles that part. The offset is the number of minutes different from GMT, so multiply that by 60 to get seconds and adjust your value to DateAdd accordingly

Related

SQL - Creating UTC Timestamp from separate date/time/timezone information

I'm working with BigQuery and have a table that looks like:
YEAR
MONTH
DAY
timezone
local time
2015
6
24
America/Los Angeles
1930
Where local time is given by hhmm. I'm wondering if I can format this information into a timestamp column in SQL that yields time in UTC.
I know I can use `TO_TIMESTAMP` but that would involve concatenating all these columns as strings first. Is there any better way to do this? If I were to concatenate, I'm not sure how I would use timezone and then back out UTC.
You might consider below.
WITH sample_table AS (
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 1930 local_time UNION ALL
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 2400 local_time
)
SELECT TIMESTAMP_SECONDS(
UNIX_SECONDS(TIMESTAMP(DATE(year, month, day), timezone))
+ DIV(local_time, 100) * 3600 + MOD(local_time, 100) * 60
) utc
FROM sample_table;
Since TIME(24, 0, 0) is not a valid time format, the query converts datetime into unix seconds and get back to UTC with the time calculation in seconds.
Input calculates to invalid time: 24:00:00
Query results

Converting only time to unixtimestamp in Hive

I have a column eventtime that only stores the time of day as string. Eg:
0445AM - means 04:45 AM. I am using the below query to convert to UNIX timestamp.
select unix_timestamp(eventtime,'hhmmaa'),eventtime from data_raw limit 10;
This seems to work fine for test data. I always thought unixtimestamp is a combination of date and time while here I only have the time. My question is what date does it consider while executing the above function? The timestamps seem to be quite small.
Unix timestamp is the bigint number of seconds from Unix epoch (1970-01-01 00:00:00 UTC). The unix time stamp is a way to track time as a running total of seconds.
select unix_timestamp('0445AM','hhmmaa') as unixtimestamp
Returns
17100
And this is exactly 4hrs, 45min converted to seconds.
select 4*60*60 + 45*60
returns 17100
And to convert it back use from_unixtime function
select from_unixtime (17100,'hhmmaa')
returns:
0445AM
If you convert using format including date, you will see it assumes the date is 1970-01-01
select from_unixtime (17100,'yyyy-MM-dd hhmmaa')
returns:
1970-01-01 0445AM
See Hive functions dosc here.
Also there is very useful site about Unix timestamp

UTC and offset date time compare in sql server

I am storing UTC datetime in Database
e.g.
2018-06-05 11:37:00.000 (UTC)
2018-06-05 17:07 (+5:30 India standart time)
I am having offset as :
offset as +02:00
How can I compare in sql query that now offset time matched ?
e.g.
2018-06-05 13:37:00.000
My issue is X (IST) date time converted to UTC and now I want to covert to different time zone (Y)
The following functions helped me to resolve the issue:
1. SWITCHOFFSET
2. TODATETIMEOFFSET
SELECT GETDATE()
SELECT SWITCHOFFSET(GETDATE(), '+05:30')
SELECT CONVERT(VARCHAR(19), SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(), '+05:30'),'+00:00'), 120)
SELECT GETUTCDATE()
If I understand your question correctly, you can use the DATEADD function to calculate the new datetime based on the UTC datetime and the offset
For example:
2 hours = 120 minutes
DATEADD(minute, 120, '2018-06-05 11:37:00.000')
Or using hours
DATEADD(hour, 2, '2018-06-05 11:37:00.000')
You can also go the other way using negative numbers
You don't have to use a literal value, you can supply a column name to parameter 3 for use in a query and also use this expression as part of a where clause
Here's a cheat sheet for the DATEADD function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017

Converting a timestamp in milliseconds in Postgres

I have a timestamp in milliseconds:
1420066991000
That translates into UTC:
Wed Dec 31 2014 23:03:11
and local time:
Thu Jan 01 2015 00:03:11
However if I try to convert it into a timestamp in Postgres, using to_timestamp, it gives me a wrong datetime:
select to_timestamp(1420066991000);
46970-02-17 13:03:20+00
Since to_timestamp, expects a double precision input, I also did this:
select to_timestamp(1420066991000.0);
46970-02-17 13:03:20+00
but the results are the same.
Am I missing something in my Postgres configuration, like some timezone setting? or is it a bug?
to_timestamp() converts unix time, that is time in seconds. Since you have data in miliseconds you should divide it by 1000.
select to_timestamp(1420066991000/1000);

Convert UTC Seconds to String in SQL

I have a column with UTC Seconds in SQL.
How can I convert UTCSeconds to varchar in order to get this output in a select statement:
DD-MMM-YYYY HH:mm:ss
Example: 1400249277 in table
16-May-2014 15:07:57 as output of Select statement
I don't have a SQLServer available right now but this looks like it might be what you're looking for : DATEADD. You are probably considering the number of seconds since January the 1st 1970, so it would be something alike :
SELECT DATEADD (second, <your number of seconds>, '1970-01-01')