Converting a timestamp in milliseconds in Postgres - sql

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);

Related

BigQuery PARSE_TIMESTAMP with unix timestamp - Failed to parse input string

I have a table that looks like this
email
created_date
me#you.com
1617753600000
you#me.com
1601510400000
bigquery tells me that created_date is stored a string. So I need to transform created_date from a unix timestamp into a date.
First I tried
PARSE_TIMESTAMP("%s", created_date) but got error: Failed to parse input string "1617753600000"
Then I tried TIMESTAMP_MICROS(CAST(created_date as int64)) as submitted_at which displays the date correctly Wed Apr 07 2021
I'm curious why does PARSE_TIMESTAMP not work in this case? Isn't this how it should be used?
You have Unix epoch time represented in milliseconds rather than the more common seconds. I recommend using the timestamp_millis() function:
select timestamp_millis(cast(created_date as int64))
Note that there are also functions for timestamp_micros() and timestamp_seconds().
%s - stands for "The number of seconds since 1970-01-01 00:00:00 UTC", but looks like you've got microseconds instead.
Try this: parse_timestamp("%s", left(created_date, 10))

convert a string to oracle sql timestamp Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)

Greeting Community.
I saw similar examples in Java but how do we do it it SQL in particular Oracle.
For now I just do this which it works because we are in PDT timezone.
-Thx for the help!
with xx as (
select replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)',' GMT-0700 (PDT)','') as dt from dual
)
select to_date(dt,'DY MON DD YYYY HH24:MI:SS') from xx
The input is a timestamp with time zone. It has redundant information about the time zone; some of that must be stripped away. For example, if you choose to trust PDT and ignore the GMT offset, you could do something like this:
with xx as (
select regexp_replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)','GMT-\d{4} \(|\)')
as str_ts_with_tz
from dual
)
select to_timestamp_tz(str_ts_with_tz,'Dy Mon DD YYYY HH24:MI:SS TZD')
as oracle_ts_with_tz
from xx
;
ORACLE_TS_WITH_TZ
---------------------------------------
2019-08-19 08:21:48 America/Los_Angeles
Note that the resulting data type is timestamp with time zone. You can further cast this as timestamp or as date (simply discarding the time zone information) if needed; this is a simple operation, internal to Oracle. But that will lose information; think twice before you do that.
Note also that the timestamp is presented in a different format in my output, compared to your input. This is because my NLS_TIMESTAMP_TZ_FORMAT is 'yyyy-mm-dd hh24:mi:ss tzr'.
If you are wondering why you must make a choice (which, then, means "why you need to delete part of the input string first") regarding the time zone information: PDT is simply not the same as GMT-0700. It may be that in summer, but not in winter. Oracle won't accept such self-contradicting nonsense as input to its functions. Either it's GMT-0700 or it's PDT, it can't be both. And, you can't just use REPLACE (not easily, anyway), because what must be replaced may have variable characters in it.

Cannot convert PSQL string to timestamp

I am trying to do a simple string to date conversion; however, PSQL complains when there is a timezone in that string. Their documentation clearly states that its supported; however, it complains. I don't even care about the timezone, I just want to convert the string.
db=> select to_timestamp('Mon Feb 23 13:43:07 PST 2015', 'Dy Mon DD HH24:MI:SS TZ YYYY')::timestamp without time zone;
ERROR: "TZ"/"tz" format patterns are not supported in to_date
Postgres version: 9.3.10
http://www.postgresql.org/docs/9.3/static/functions-formatting.html
Why not try a straight cast?
SELECT 'Mon Feb 23 13:43:07 PST 2015'::timestamp with time zone
Once you've done this it's pretty easy to work with the date object.

Extract date,month,year and month name from the unix timestamp with postgresql

I use postgres for the rails app and I have a unix timestamp in postgresql db. I have a requirement to select and group by the dd-mm-yyyy and by month name.
Consider I have the following unix timestamp
1425148200
and I would need to change this to datetime and I used to_timestamp which returned
2015-02-28 18:30:00 UTC
and I tried to convert the datetime to local timezone using
::timestamp without time zone AT TIME ZONE 'IST'
but that did not give time in required timezone and instead it returned
2015-02-28 16:30:00 UTC
and I tried to get the date part using ::date which returned
Sat, 28 Feb 2015
So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp.
Thanks in Advance!
select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')
01-03-2015 & of course March
It is postgres mistake I guess
according to http://www.postgresql.org/docs/7.2/static/timezones.html

SQL: Convert GMT in seconds to DateTime

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