Modify timestamp - postgresql

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

Related

How to change the timestamp format in Postgresql to extract day part of the str?

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.

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.

Convert to_timestamp in 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);

RFC3339 format in PostgreSQL

I store a time as VARCHAR(30) as we can see it below:
I know it is far from best practices.. there is some way to convert such a string into PostgreSQL's time ?
Simply casting can do the trick like:
SELECT time::timestamptz FROM table;
Proof:
SELECT '2016-08-12T15:15:01.100001Z'::timestamptz;
timestamptz
-------------------------------
2016-08-12 15:15:01.100001+00
(1 row)

Postgresql How extract date

I need get only the date from now() at my time zone, I have this query:
SELECT now() AT TIME ZONE 'America/Santiago'
And I'm getting something like this "2015-06-08 23:59:34.142569"
but I need extract only the date, how can I get it?
Thanks.
If you want the server's date,
SELECT current_date;
If you need the date for any timestamp, eg the one you've gotten into your timezone, use date().
SELECT date(now() AT TIME ZONE 'America/Santiago');
Docs: http://www.postgresql.org/docs/current/static/functions-datetime.html
For postgres you want
select current_date;
if you need to extract any of those fields out of the returned value you can use extract
EXTRACT (field FROM source)