How do I covert epoch time value into a timestamp(6) in teradata ?
Lets take 1336987231051 as the example epoch time ( note this is in milliseconds, where as epoch time is in seconds), I did something like this
// miliseconds epoch time
select cast(1336987231051 as timestamp(6))
// seconds epoch time
select cast((1336987231051/1000) as timestamp(6))
and I get this error message for both of the above select statements :
[Error] Script lines: 12-12 ------------------------
[Teradata Database] [TeraJDBC 13.10.00.31] [Error 5407] [SQLState HY000] Invalid operation for DateTime or Interval.
I verified in http://www.epochconverter.com/ that 1336987231051 is valid epoch time.
What is the right sql for this in teradata ?
select
cast(cast(700101 as date) + seconds_from_epoch / 86400 as timestamp(6)) +
(seconds_from_epoch mod 86400) * interval '00:00:01' hour to second
from my_table
Simplest way to convert EPOCH to TERADATA TIME.
SELECT
1336987231051/1000 as unix_epoc_time ,
to_timestamp(unix_epoc_time) utc,
cast(cast(utc as char(19))||'+00:00' as timestamp(0) with time zone) AT LOCAL
;
If your epoch is 10 digit, you don't need /1000.
Remember, Unix time is in UTC.
Your system will treat this "utc" as local. So, let us make it understand that it is UTC by adding "+00:00" and then convert it to your LOCAL using AT LOCAL OR using either of these "America Central" , "America Eastern" , "America Mountain" etc.
Related
I am using a H2 Database where i need to convert the Epoch to Datetime (24 hours) format. I have the below sample detail.
TABLE: MTTR
COLUMN : END_TIME
VALUE : 1659946913581
I have the below query ,
select
to_char((DATEADD('SECOND', ((END_TIME/ 1000) + 19800), DATE '1970-01-01')), 'yyyy-mm-dd hh:mi:ss') AS END_TIME_VAL
from MTTR;
The issue is that it is giving the incorrect value as '2022-08-08 01:51:53'. It should be '2022-08-08 13:51:53'. What changes can be done here ?
If you expect 2022-08-08 13:51:53, your time zone has +05 offset from UTC, so you can use something like
-- This command isn't needed if your connection already has the proper time zone
SET TIME ZONE '+05';
SELECT END_TIME * INTERVAL '0.001' SECOND + TIMESTAMP '1970-01-01 05:00:00' FROM MTTR;
in all recent versions of H2 database.
I have timestamp in following format: "2022-03-08 17:11:59.901" , I want to convert them to UNIX/Epoch format up to milliseconds, somehow it only delivers till seconds part.
Is there anyway to do it?
If you have a string timestamp and you want to convert to EPOCH:
SELECT to_timestamp('2022-03-08 17:11:59.901') as ts,
date_part(EPOCH_MICROSECOND ,ts)
I have a timestamp in epoch format like this: 1551187548876. If I convert this to a timestamp using https://www.epochconverter.com/, I get the correct time back.
However, if I convert it to time using PostgreSQL to_timestamp, I get the following:
select to_timestamp(1551187548876) from my_table;
51125-03-06 14:14:36+00
Question
How can I convert epoch 1551187548876 to a timestamp in PostgreSQL?
I assume that the epoch number you have is the number of milliseconds since the epoch, not the number of seconds, as customary.
Try dividing by 1000:
select to_timestamp(1551187548.876);
to_timestamp
----------------------------
2019-02-26 14:25:48.876+01
(1 row)
why i get error with this code
SELECT EXTRACT(SECOND FROM TIME total_time) from tr_empl_quiz;
and i got error to with this code
SELECT EXTRACT(EPOCH FROM TIMESTAMP total_time) from tr_empl_quiz;
this is my table content tr_empl_quiz and type data is time without time zone
total_time
============
00:01:00
00:02:00
When you use the extract() function you are getting the value of the date/time part. In your examples, the seconds are zero, so that is what you get.
Postgres does support what you want, using the perhaps unintuitive name epoch. Epoch returns the number of seconds. For an date or datetime value, this is the number since 1970-01-01 (the beginning of Unix time). For a time or interval it is the total number of seconds during the period. So:
select extract(epoch from time '00:02:00')
returns 120.
Surprisingly, the documentation for epoch doesn't mention that it works on the time data type. The functionality is entirely consistent with what the function does. Either the documentation (which is generally quite excellent) overlooks time; or time is treated as an interval.
For a column in a table, you would just do:
SELECT EXTRACT(EPOCH FROM total_time)
FROM tr_empl_quiz;
or:
SELECT EXTRACT(EPOCH FROM CAST(total_time as time))
FROM tr_empl_quiz;
Depending on what you want.
I am wondering is there a way to select only rows where time since epoch and "now()" is greater than a certain amount of seconds.
I store the rows with a field holding seconds since epoch created from the php function time().
So something like this:
SELECT * FROM table WHERE (now - field_time) > 60 seconds
NOW() returns a SQL DateTime and what you need is UNIX_TIMESTAMP() which returns seconds since epoch (unix timestamp) for now if you don't give any date as parameter.