PostgreSQL - how to convert string to date with universal format - postgresql

Is there universal way to convert a string to date in PostgreSQL if you don’t know the string format in advance?
For example: the string date may be in yyyy-mm-dd or dd-mm-yyyy format or something else.
Thanks!

Try typecasting the string to timestamp and then date.
Something like:
select '2010-01-01 12:00:00'::timestamp::date

There is no universal parameter to handle any date format. You have the DateStyle parameter and
and the date/time interpretation rules.

Related

Date formatting in postgresql from existing data

I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')

How do I create a specific date in PostgreSQL?

I need to execute a INSERT statement writing a date with a YYYY-MM-DD format.
Would to_date('2021-09-28','YYYY-MM-DD') work?
YYYY-MM-DD is the the ISO 8601 standard date format and unambiguous default in Postgres. Just insert your date literally.
The type date is stored as a 4-byte integer quantity internally, which does not preserve any format. You can format any way you like on output with some basic locale settings or settings of your client, or explicitly with to_char().
Input with to_date('2021-09-28','YYYY-MM-DD') works, too. But you don't need to_date() while operating with ISO format.

SQLite Convert to SQLite Datetime Format from ANY datetime format

Coming out of an Oracle background converting dates from any format to any format is really easy.
How is this done in SQLite? I've searched and searched for answers and most of the answers simply say... Save your date/strings in SQLite in one single format which is YYYY-MM-DD HH:MM:SS.SSS. This seems rigid to me.
I don't have that luxury as my data is stored in this format DD/MM/YYYY HH:MI:SS am ex. 3/7/2020 8:02:31 AM.
NOTE: For single days/months my date values do not contain leading zeros and my time is NOT in military time.
How do I tell SQLite what my date format is so that I can correctly convert my stored dates to SQLite datetime formats?
Convert from SQLite Date Format to Oracle Date Example:
In Oracle I would simply use the to_date function like so
to_date('2019-03-07 15:39:34', 'YYYY-MM-DD HH24:MI:SS')
All one needs to do is to tell the function what the date format is... and then it spits out a date.... easy peasy. What this example does is convert a SQLite date formated string to a date that Oracle recognizes as a date. It doesn't matter what the format is in as I tell the function what format to expect.
How do I Convert Dates in SQLite from any format to the SQLite Format?
Converting from SQLite's date format string to ANY date is easy as there are functions built in that do this easily... but how to do this the other way round?

Date stored in different format

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app.
In Spring, the field date is as follows :
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
The date is well stored, but with this format YYYY-MM-dd.
Is there anything that I'm missing ?
MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.
You probably have to specify the date format going out to the storage, as #JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still
Formats a date in the date escape format yyyy-mm-dd.
according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.
You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

Teradata character to date conversion

I have a string associated with date in ‘Teradata’ tables
Var1=09OCT2017-EMRT
I need to extract the date from the above string in ‘mm/dd/yyyy’ format
I tried the following
Cast(cast(substr(var1,1,9) as char(20)) as date format ‘mm/dd/yyyy’) as date
I am getting error as ‘invalid date supplied for var1’
I would appreciate your help
You need to apply a format matching the input string:
To_Date(Substr(var1,1,9), 'ddmonyyyy')
returns a DATE.
If you want to cast it back to a string:
To_Char(To_Date(Substr(var1,1,9), 'ddmonyyyy'), 'mm/dd/yyyy')