converting string with offset to timestamp in postgresql - postgresql

how do I convert
'25/02/2014 10:49:13 -8'(datatype is VARCHAR)
to
'2014-02-25 13:50:13.000000'(datatype is TIMESTAMP)
in postgreSQL
I believe the timestamp is EST timezone.

Try the method
to_timestamp
Example:
to_timestamp('01 Jan 2007 09:09:09', 'DD Mon YYYY HH:MI:SS')
Hope This helps

Related

Datetime with meridian indicator convert from Oracle to Postgresql

How to insert "29-SEP-22 12.00.17.225 AM" in PostgreSQL DB ?
In Oracle Table,
"29-SEP-22 12.00.17.225 AM"
In PostgreSQL Table , ??
How to capture above date format in PostgreSQL table ?
what will be the datatype PostgreSQL side ?
The data type would be timestamp. Use to_timestamp with format specification dd-mon-yy hh12.mi.ss.ms pm.
Example:
select to_timestamp('29-SEP-22 12.00.17.225 AM', 'dd-mon-yy hh12.mi.ss.ms pm');

Postgres: Display timestamp in specific UTC format

I have a timestamp stored this way in database:
2021-08-14 12:19:58+00
I need to display it in this format
2021-08-14 12:19:58 UTC
Use the to_char function with the TZ format:
SELECT to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS TZ');
to_char
══════════════════════════
2021-09-21 23:06:15 CEST
(1 row)

How to get current month in string and year in Postgresql

I want to show date in string format as current month name(String) and year(e.g -> SEPTEMBER, 2019). How to write query for this in PostgreSQL?
I tried this query:
select
date(date_trunc('month', current_date));
but it gives me only current months starting date.
Try to_char() and add the year formatter to the string like this:
SELECT to_char(current_date, 'MONTH YYYY')
This will return:
SEPTEMBER 2019
Here's a sqlfiddle
If you want to format the output of a DATE value, use to_char()
select to_char(current_date, 'Month, yyyy');
we can do it like this also in Postgresql -
SELECT TO_CHAR(NOW() :: DATE, 'Month , yyyy');
output - September, 2019

Convert text value ( 6/1/2018 11:34:33 PM , 6/2/2018 2:32:07 AM) to timestamp

I have a column with text datatype in PostgreSQL with values below:
6/1/2018 11:34:33 PM
6/2/2018 2:32:07 AM
I want to convert it to timestamp:
2018-01-06 23:34:333
2018-02-06 02:32:07
How do I do it?
I tried to_timestamp(entry_time_stamp, 'YYYY-MM-DD HH24:mi:ss') but it does not add PM value to it.
use :
to_timestamp(entry_time_stamp, 'YYYY-MM-DD HH12:mi:ss AM')

Casting a string to Date and Time in PostgreSQL

I have a table in GreenPlum (PostgreSQL) with all fields as sting, and I want to edit the types :
To do this I created a view :
CREATE VIEW typed_view AS
SELECT CAST(sid AS bigint), CAST(gid AS bigint),
...
But I have a problem with the Date and Time fields, I tried this command but it didn't work :
to_utc_timestamp(from_unixtime(unix_timestamp(eventdatetime,"yyyy-MM-dd
HH:mm:ss")),'UTC') AS eventdatetime,
After that I tried the PostgreSQL notation :
to_timestamp(eventdatetime, 'YYYY Mon DD HH24 MI SS') AS eventdatetime,
But still not working.
Anyone knows how to convert it ?
I also have this command that is not working :
CASE WHEN fix = "True" THEN TRUE ELSE FALSE END AS fix,
Thanks in advance
You didn't provide example data so I'm going to assume your data looks like "YYYY Mon DD HH24 MI SS". So January 4, 2016 at 2:15:20 PM would look like '2016 Jan 04 14 15 20' in your data. So with this example data, the conversion would look like this:
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss') as col1;
col1
------------------------
2016-01-04 14:15:20-05
(1 row)
Now this is a timestamp which also include the timezone offset which for my server is -5. To convert this to a timestamp without the timezone, you just add ::timestamptz.
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss')::timestamp as col1;
col1
---------------------
2016-01-04 14:15:20
(1 row)
A very important note on this. It is costly to convert data from a string to a different datatype. That is the same in all databases too. It is better to incur the expense of this conversion once rather than doing it for every SELECT statement. So, I also suggest you materialize this transformation into a physical table rather than using a VIEW.