Read Postgres Text column values as timestamp converted to epoch integers - postgresql

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.

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.

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

Convert bigint data type to timestamp (and subsequently to date) in redshift

I need to convert the value stored in a bigint column to a date field. The first step of the conversion involves converting it to timestamp, and subsequently use the TRUNC method to convert this column to a date value.
However, my query is failing while converting the bigint value to timestamp.
The error that I'm getting is:-
Amazon Invalid operation: cannot cast type bigint to timestamp without time zone;
The query I'm trying for now is something like this:-
select ts::timestamp from events limit 1;
I was able to avoid the time zone error by using the method described in this thread: https://stackoverflow.com/a/36399361
My dates are based on epochs, and I was able to do the following:
SELECT
(TIMESTAMP 'epoch' + contract_start_date * INTERVAL '1 Second ')
FROM
table_name
SELECT TIMESTAMP 'epoch' + {column of bigint}/1000 * INTERVAL '1 second' as adate FROM tbl
If you are starting with a POSIX timestamp, and trying to get a timezone aware datetime value, you will need to supply a timezone - even if you later want to truncate the time part away. I'm not familiar with redshift, but perhaps there is a way to specify you mean UTC.

Default timestamp format and fractional seconds

I'm trying to format the timestamps in my Postgres database to a certain format:
YYYY-MM-DD HH24:MI:SS
By doing:
update myTable set tds = to_char(tds, 'YYYY-MM-DD HH24:MI:SS')::timestamp;
I managed to set all the previously stored tds to this format. However, any newly added entry goes back to: YYYY-MM-DD HH24:MI:SS.MS since the default is set to now().
How do I change this so that newly added entries also have the format: YYYY-MM-DD HH24:MI:SS?
There is no format stored in a timestamp type. You can set its default to a timestamp truncated to the second at creation time
create table t (
tds timestamp default date_trunc('second', now())
)
Or alter the table
alter table t
alter column tds
set default date_trunc('second', now());
insert into t values (default);
INSERT 0 1
select * from t;
tds
---------------------
2014-03-11 19:24:11
If you just don't want to show the milliseconds part format the output
select to_char(now(), 'YYYY-MM-DD HH24:MI:SS');
to_char
---------------------
2014-03-11 19:39:40
The types timestamp or timestamptz optionally take a precision modifier p: timestamp(p).
To round to full seconds, set the default to:
now()::timestamp(0)
or:
now()::timestamptz(0)
Standard SQL functions CURRENT_TIMESTAMP (returns timestamptz) or LOCALTIMESTAMP (returns timestamp) allow the same precision modifier:
CURRENT_TIMESTAMP(0)
LOCALTIMESTAMP(0)
That's a bit shorter than calling date_trunc() - which truncates fractional seconds (may be what you really want!)
date_trunc('second', now())
Store timestamps as timestamptz (or timestamp), not as character type.
Finally, to make sure that ...
newly added entries also have the format: YYYY-MM-DD HH24:MI:SS
you could define your column as type timestamptz(0). This covers all values entered into that column, not just the default. But the rounding may introduce timestamps up to half a second in the future. If that can be an issue in any way, rather use date_trunc().
See #Clodoaldo's answer for instructions on to_char() and how to ALTER TABLE.
This related answer for in-depth information on timestamps and time zone handling:
Ignoring time zones altogether in Rails and PostgreSQL

Datatype of Hour in oracle

When we use the query like this in oracle to get the the total number of hours in round figure,
hours= select round(out_time-in_time)*24 from table_name;
what is the datatype of hours here?
out_time and in_time are column names
You can take NUMBER as the datatype in your case.
EDIT:- This will return you an integer
select 24 * round((to_date('2013-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2013-07-07 19:30', 'YYYY-MM-DD hh24:mi'))) diff_hours
from table_name;
Maybe a datatype called "INTERVAL" will suit you. It's called:
INTERVAL DAY [(day_precision)] TO SECOND
Stores a period of time in days, hours, minutes, and seconds
Interval is something that can we added (or substrated) to a DATE or TIMESTAMP.
see: http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm