Postgres timestamp formatting in query - postgresql

I am migrating a code with its own ORM from Db2 to postgres. I have a query that executes the following sql on postgres 10 -
SELECT * FROM TriggerQueue
WHERE TriggerQueue.atServerStartup = 'Y'
AND (TriggerQueue.scheduledatetime > '2018-06-21 20.02.57.827' OR
TriggerQueue.scheduleDateTime is null) AND TriggerQueue.inputQueue = 'N'
but the pgadmin is showing the following error:
ERROR: invalid input syntax for type timestamp: "2018-06-21
20.02.57.827"
LINE 3: AND (TriggerQueue.scheduledatetime > '2018-06-21 20.02.57.8...
^
SQL state: 22007
I'm guessing the timestamp format is wrong based on sql state, but I'm not sure how to format the value. Any insight on this would be very helpful.
EDIT : Timestamp field in pg is of the type timestamp without timezone. Pgadmin shows size of 6.

Use a proper timestamp literal:
timestamp '2018-06-21 20:02:57.827'
Note the : to separate hours, minutes and seconds

Related

Swapping Timestamp for Timestamp With Time zone causes error with jdbc

I'm looking for a way around an unexpected problem. We have a third party app that is loading data into our database. It's our code that creates the postgresql schema, the third party doesn't provide it. With TIMESTAMP columns this works, but we'd much prefer to use TIMESTAMP WITH TIME ZONE.
We tried to do this by changing the default TimeZone of the user. The following SQL works through psql. The fixed timezone for the user is assumed:
insert into AAA (FOO,BAR) select 'X','2022/03/19 21:15:25' where not exists (select 1 from AAA where FOO = 'X'
But the app is raising an exception:
Caused by: org.postgresql.util.PSQLException: ERROR: column "BAR" is of type timestamp with time zone but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
The timezone of the user was set with:
insert into pg_db_role_setting (setrole, setdatabase, setconfig) values (16394, 0, '{TimeZone=+10}')
And in psql, subsiquent calls to SELECT now() return in +10 timezone as desired.
Firstly, why could JDBC be hitting an error when the psql doesn't on the same insert statement?
Secondly is there any workaround for this without changing the program source code?

Postgres error when parsing date with timezone

I have a table with a bunch of records, with different values for a date and I need them all parsed as a date value, so I'm trying to parse a date in postgres and I'm receiving an error which doesn't tell me much
select to_Date(:original_date, 'YYYYmmDD');
When I pass this value to original_date is when I get the error: '2022-11-18T11:02:08-03:00'
Here's the error I'm getting:
SQL Error [22008]: ERROR: date/time field value out of range: "2022-11-18T11:02:08-03:00"
Where: SQL statement "select to_Date(original_date, 'YYYYmmDD')"
PL/pgSQL function parse_date(character varying) line 5 at SQL statement
As mentioned by Hambone in the comment below the question, changing my date format to 'YYYY-mm-DD' works like a charm.
Thanks for that Hambone!

Insert data to postgres table

I'm Trying to insert the data from csv file which was exported from Oracle DB. when I try to import on PGadmin. its failing with below error.
ERROR: invalid input syntax for type timestamp: "29-APR-18
12.04.07.000000000 AM" CONTEXT: COPY consolidated_dtls_job_log, line 1, column start_time: "29-APR-18 12.04.07.000000000 AM"
Note: Column Start_time is created with timestamp datatype.
Use a different NLS_TIMESTAMP_TZ_FORMAT when exporting the data from Oracle; something that is closer to the ISO format.
Here is an SQL statement provided by Belayer:
ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF TZH:TZM';

How to insert current datetime in postgresql insert query [duplicate]

This question already has answers here:
in postgres, can you set the default formatting for a timestamp, by session or globally?
(3 answers)
Closed 6 years ago.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
This is the query I have used for mysql to insert current date time.
When I am using this in postgresql, I am getting below error.
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function utc_timestamp() does not exist
SQL state: 42883
I have tried like below using now(), however it is inserting like "2016-07-07 17:01:18.410677". I need to insert in 'yyyymmdd hh:mi:ss tt' format.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
How to insert current date time in insert query of postgresql in above format ?
timestamp (or date or time columns) do NOT have "a format".
Any formatting you see is applied by the SQL client you are using.
To insert the current time use current_timestamp as documented in the manual:
INSERT into "Group" (name,createddate)
VALUES ('Test', current_timestamp);
To display that value in a different format change the configuration of your SQL client or format the value when SELECTing the data:
select name, to_char(createddate, 'yyyymmdd hh:mi:ss tt') as created_date
from "Group"
For psql (the default command line client) you can configure the display format through the configuration parameter DateStyle: https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE
For current datetime, you can use now() function in postgresql insert query.
You can also refer following link.
insert statement in postgres for data type timestamp without time zone NOT NULL,.
You can of course format the result of current_timestamp().
Please have a look at the various formatting functions in the official documentation.

Insert throws SQLCODE: -180, SQLSTATE: 22007 on IBM DB2

I'm using a data integration tool (Talend) to insert some rows on a IBM Db2, but the following command throws an error:
INSERT INTO "TOTCHQ" ("CODREM","NUMDOCC8","NOMPES","NUMBAN","CODAGEBN","QTDCHQDL","DATULTOA") VALUES ('3080',99999999999,'FULANO DE TAL',100,'0000',2,'2012-11-28')
DB2 SQL error: SQLCODE: -180, SQLSTATE: 22007, SQLERRMC: null - Line: 0
Seems like the date column (DATULTOA) is in wrong format, but it isn't.
The same command, when executed on another tool (like DbVisualizer) or even directly, runs ok.
Here is my table metadata:
CODREM VARCHAR(4)
NUMDOCC8 DECIMAL(14, 0)
NOMPES VARCHAR(50)
NUMBAN SMALLINT
CODAGEBN VARCHAR(6)
QTDCHQDL SMALLINT
DATULTOA DATE
Thanks in advance.
The description of SQLCODE -180 is "THE DATE, TIME, OR TIMESTAMP VALUE value IS INVALID".
I count 7 column names and 7 data values. So, it's not a misalignment problem.
The date '2012-11-28' assumes a yyyy-mm-dd format. Perhaps talend is expecting a yyyy-dd-mm format, or some other date format.
Make sure date format is right :
example : yyyy-MM-dd