Postgres error when parsing date with timezone - postgresql

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!

Related

Generated column from date column in postgresql 10

Having a problem with this, trying to add a column which returns the year from a date column in postgresql 10: This is my SQL
ALTER TABLE stats ADD COLUMN year_ integer GENERATED ALWAYS AS (date_part('year', date)) STORED;
However, this returns a syntax error in the SQL window. I have looked up the online documentation and have no idea what is going wrong, also tried with the EXTRACT function and I keep on getting the same error of the sort
ERROR: syntax error at or near "("
LINE 1: ...tats ADD COLUMN year_ integer GENERATED ALWAYS AS (date_part...

Casting String type to Unix Date Amazon Athena

I'm looking to get a result in Amazon Athena were I can count the quantity of users created by day (or maybe by month)
But previous that I have to convert the unix timestamp to another date format. And this is where i fail.
My last goal is to convert this type of timestamp:
1531888605109
In something like:
2018-07-18
According to Epoch Converter
But when I try to apply the solution i saw in this quiestion: Casting unix time to date in Presto
I got the error:
[Simba]AthenaJDBC An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:13: Unexpected parameters (varchar) for function from_unixtime. Expected: from_unixtime(double) , from_unixtime(double, bigint, bigint) , from_unixtime(double, varchar(x)) [SQL State=HY000, DB Errorcode=100071]
This is my query:
select cast(from_unixtime(created)as date) as date_creation,
count(created)
from datalake.test
group by date_creation
Maybe I've to cast over the sctring because the data type of the field is not a date.
My table description: Link to the table description
line 1:13: Unexpected parameters (varchar) for function from_unixtime. Expected: from_unixtime(double)
This means that your timestamps -- even though they appear numeric -- are varchars.
You need to add a CAST to cast(from_unixtime(created)as date), like:
CAST(from_unixtime(CAST(created AS bigint)) AS date)
Note: When dealing with time-related data, please have in mind that https://github.com/prestosql/presto/issues/37 is not resolved yet in Presto.

Postgres timestamp formatting in query

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

How to cast date inside age() in postgresql

I have a column as date (CHARACTER VARYING) in table And it may contain dd/mm/yyyy or mm/dd/yyyy format dates. Now i need to convert all dates in dd/mm/yyyy ..
select c.trial_id,c.name,c.gender,age(cast(c.dob as date)) as p_age,c.relationship_name,c.relationship,c.dob,c.staff_id from mas_patient_details c
Even i tried doing like this
select to_char(dob::date, 'DD/MM/YYYY')from mas_patient_details
both of query returns
ERROR: date/time field value out of range: "12/21/1989" HINT:
Perhaps you need a different "datestyle" setting.
********** Error **********
ERROR: date/time field value out of range: "12/21/1989" SQL state:
22008 Hint: Perhaps you need a different "datestyle" setting.

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.