Convert to_timestamp in postgreSQL - postgresql

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

Related

Read Postgres Text column values as timestamp converted to epoch integers

I have a postgres database carrying date/time information in a text format. There is no way of changing it, but I have to retrieve those values as milisecons since epoch.
I managed to make a query, converting those date-time records to timestamps so that I get a correct "max" function behaviour like so:
SELECT max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
But converting other results into miliseconds does not seem to work. And all the examples int the documentation and forums showcase only the usage for some literal value, not a value selected from a database. So lines like these don't work:
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select TO_TIMESTAMP(column_name,'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE(
SELECT TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE TO_TIMESTAMP
(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
Is there an actual way to accomplish what I want by using a query, or I have to do something more complicated?
P.S.
Of course I can just retrieve all the infomation as text and use Qt (QDateTime) to convert it to miliseconds, but It would be more expensive and I was wondering if there is a way to ask the database to do it for me.
The timestamp keyword is only needed for literals (constants), not if you have a proper timestamp value available:
SELECT extract(epoch from max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')))
FROM table_name;
Note that epoch represents seconds, not milliseconds.

How to remove the tail from timestamp field?

I have a TIMESTAMP WITHOUT TIME ZONE field
when I do:
select datex
from A
it shows me datex as: 2015-09-16 10:59:59.073629
how do I modify it to be 2015-09-16 10:59:59?
I don't need the tail after the seconds.
I read this http://www.postgresql.org/docs/9.1/static/functions-datetime.html but coldn't find a match.
You can either configure your SQL client tool to not show fractional seconds, or you can use the to_char() method to format the output:
select to_char(datex, 'yyyy-mm-dd hh24:mi:ss') as datex
from A;
See the manual for details on the format string. This is documented in the chapter "Data Type Formatting Functions":
http://www.postgresql.org/docs/current/static/functions-formatting.html
try:
SELECT date_trunc('second', datex);
The function date_trunc is similar to the trunc function for numbers.

How to convert date time into unix epoch value in Postgres?

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

Modify timestamp

I have a timestamp like 2014-08-17T06:16:55.967Z that I want to insert in a Postgres db, but I want to remove just the milliseconds and keep the 'T' and the 'Z'. Anyone know how that is possible? I've tried 2014-08-17T06:16:55.967Z::timestamp(0) or timestamptz(0), but they both take away what I want to keep.
I would like 2014-08-17T06:16:55Z.
date_trunc
select date_trunc('second', '2014-08-17T06:16:55.967Z'::timestamp);
date_trunc
---------------------
2014-08-17 06:16:55
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
The T and Z in the input string are not saved in the timestamp column. If you want to show them then format the output
select to_char(
date_trunc('second', '2014-08-17T06:16:55.967Z'::timestamp),
'YYYY-MM-DD"T"HH24:MI:SSZ'
);
to_char
----------------------
2014-08-17T06:16:55Z
http://www.postgresql.org/docs/current/static/functions-formatting.html

How to truncate seconds in TSQL?

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)