How to remove the tail from timestamp field? - postgresql

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.

Related

PostgreSQL timestamp Grammer

SELECT * FROM items WHERE created_time >= 20210505143012999
on mySQL, we can give condition like WHERE created_time >= 20210505143012999.
but, I want to find it with similar format(20210505143012999) on PostgreSQL.. How can I do this?
Seems mySQL is a little lax with data types (or perhaps just more forgiving), in Postgres your value is just an number (bigint). You need to convert it with the to_timestamp function. But as an epoch it seems Postgres does not like and it also does not appear to be an epoch either. You can either pre-convert to a string then use the to_timestamp of cast it as test within the function parameters. Either way specify the format: (see demo)
select to_timestamp ('20210505143012999', 'yyyymmddhh24missms');
select to_timestamp (20210505143012999::text, 'yyyymmddhh24missms');

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

date and time concatenation in postgres

I wanted to get the only the hour of the time and concatenate it with the date.
here's my query
SELECT distinct TOTALIZER_METER_READINGS.date + to_char(TOTALIZER_METER_READINGS.time ,'HH')
FROM TOTALIZER_METER_READINGS
is there any other way to get the hour of the time without turning it into text?
+ is the operator to add numbers, dates or intervals.
The string concatenation operator in SQL is ||.
As you are storing date and time in two columns rather then using a single timestamp column, I would convert them to a single timestamp value, then apply to_char() on the "complete" timestamp:
Adding a time to a date returns a timestamp that can then be formatted as you want:
SELECT distinct to_char(TOTALIZER_METER_READINGS.date + TOTALIZER_METER_READINGS.time, 'yyyy-mm-dd HH')
FROM TOTALIZER_METER_READINGS
You can use EXTRACT (or the date_part function):
SELECT EXTRACT(hour FROM current_timestamp);
The result type is double precision.

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.