How do I convert the following format to UNIX timestamps?
A value like: 01-02-2015 10:20 PM should be converted to: 1418273999000
I did try to_timestamp function but its not working for me.
If your data is stored in a column called ts, in a table called data, do this:
select extract(epoch from ts) from data
To add Joe's answer, you can use date_part, i think it's syntax is clearer than 'extract'.
select date_part('epoch', ts) from data;
Adding to haoming answer,
for UNIX epoch this was my approach.
I also added a 180 day interval which can be changed/removed upon requirements.
date_part('epoch', (column_name + INTERVAL '180 day')) * 1000
Related
I have create a datetime with type timestamp. datetime timestamp NOT NULL I am not sure why the output is like this:
I want to extract the day part. I have tried these different approach but in both cases I am getting an error. How can I fix it?
extract(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI'))::timestamp)
EXTRACT(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
date_part('day', min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
As mentioned in response I modified query to be like below and it does work.
extract(day from MIN(datetime)) as Day
All you need is:
select *, extract(day from activated_at) as Day from yourTable;
What you are seeing is a timestamp formatted as text for the display. Underlying data is timestamp as you said, directly use it.
I'm querying the database (RedShift) and I have a piece of information stored in epoch MS format. Envision a table along the lines of:
Purchase, date
1, 1620140227019
2, 1620140227045
3, 1620140226573
I need to convert the timestamp to a readable date but I can't make it work with to_timestamp() or extract(). The problem is first with the size of the value (13 digits are not supported).
The closest solution I have is
select to_timestamp(1620140226573/1000, 'SS')
But the result is 0051-05-04 14:57:06. In other words month, date and seconds are correct but the year is wrong.
You can run this query
select to_timestamp(round(1620140227254/1000))
The solution was in the documentation: https://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
SELECT timestamp with time zone 'epoch' + 1620140227019/1000 * interval '1 second' AS converted_timestamp
or
select '1970-01-01'::date + 1620140227019/1000 * interval '1 second'
I can do this to get UTC
select CURRENT TIMESTAMP - CURRENT TIMEZONE AS utc
FROM SYSIBM.SYSDUMMY1;
one would imagine something like
select GetEpoch(CURRENT TIMESTAMP - CURRENT TIMEZONE) AS utc
FROM SYSIBM.SYSDUMMY1;
what would be the logic for GetEpoch?
If your Db2-database lives either on Linux/Unix/Windows or i-series and is currently supported by IBM, then you can use the EXTRACT function which accepts an EPOCH parameter (among other things).
For EPOCH, Extract Returns The number of seconds since 1970-01-01 00:00:00.00 for the supplied date/timestamp expression.
Example values extract(epoch from current timestamp) or select extract(epoch from current timestamp) from sysibm.sysdummy1
Db2-LUW documentation here.
Db2-for-i documentation here.
Although the EXTRACT function is available on Db2-for-Z/OS, it does not appear to accept the EPOCH parameter, unless undocumented, documentation here.
The following query returns the same result in both rows:
VALUES
extract(epoch from current timestamp)
, (days(current timestamp) - days(timestamp('1970-01-01'))) * 86400 + midnight_seconds(current timestamp)
Lets do the following:
SELECT to_timestamp(1453336500)::date
Then i get a date 2016-01-21
How does the function work backwards. With the date as input and the number (i guess seconds from 1970) as result?
You use extract:
SELECT extract(epoch FROM current_timestamp);
I have time, select cast(SYSDATETIME() AS time)
14:59:09.2834595
What is the way to truncate seconds?
14:59
Description
You can use the T-SQL function convert.
Sample
PRINT convert(varchar(5), SYSDATETIME(), 108)
will give you hh:mm
More Information
MSDN - CAST and CONVERT
If you want to truncate seconds and still have a T-SQL Date datatype, first convert the date into minutes from the date '0' and then add the minutes back to '0'. This answer doesn't require any additional parsing/converting. This method works to truncate other parts just change MINUTE.
Example:
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, '2016-01-01 23:22:56.997'), 0)
If you need to drop seconds off entirely, you can use the DATEPART() function (SQL Server) to strip out the hour and minute, then append it back together. (I like dknaack's solution more, if that works.)
SELECT CAST(DATEPART(hour, SYSDATETIME()) + ':' + DATEPART(minute, SYSDATETIME()) AS DATETIME)
select cast(left(cast(SYSDATETIME() AS time), 5) as time)