Convert a varchar column to date for the month of December - varchar

I have a varchar2 column in a table which saves dates in it with the format mask 'DD-MON-RR'.
Now when I try to convert it with
{to_date(my_field,'DD-MON-RR')}
it returns accurate result except when I convert the December month, it throws an error
literal does not match the format string
I have solved my problem by converting whole string to {to_char} again, but my question is: why does Oracle not let me convert the month of December saved in form of 'DEC'?

Related

Convert to required date format with date datatype

I have input col as 12/03/08 viz dd/MM/yy . So I beed it in the format of dd/MM/yyyy . SO I have use the below transformation as :
toString(toDate(col,'dd/MM/yy'),'dd/MM/yyyy')
This works fine.
But at the end i need to conver this col to date datatype with the same format as dd/MM/yyyy . When i do the CAST transformation like
In cast , i have converted it to date and given the format as dd/MM/yyyy .Its giving the date but in default format like dd-MM-yyyy . I need slashes instead of dashes.
I tried to do
toDate( col, 'dd/MM/yyyy') ,Still i get the default format dd-MM-yyyy.
How to fix this.
When you CAST in SQL for conversion, the resulted format is determined by the default format of your database. If you want to ensure that the date is formatted in a specific way, you can use the DATE_FORMAT function in combination with the toDate function.
If you want to get the wanted dd/MM/yy to the format dd/MM/yyyy, you can use the following transformation:
toString(toDate(col, 'dd/MM/yy'), 'dd/MM/yyyy')
This first converts the string to a type using toDate and then formats itt as a string with the wanted format using the toString function.
However, if you then want to convert the string column back to a date datatype with the format dd/MM/yyyy, you cant simply use the CAST function with the desired format, as the resulting format will still be determined by the default format of your database. Instead, you can use the DATE_FORMAT function to format the date as a string with the desired format, and then convert it back to a date datatype using the toDate function.
To convert the string column col back to a date datatype with the format dd/MM/yyyy, you can use the following transformation:
toDate(DATE_FORMAT(col, 'dd/MM/yyyy'), 'dd/MM/yyyy')
This first formats the date in your string column as a string with the desired format using the DATE_FORMAT function, and then converts it back to a date datatype with the same format using the toDate function.

How to extract the year and quarter from the String date in databricks SQL

Can someone show me how to extract the year from the String date in databricks SQL.
I am based in the UK and our date format is normally as follows:
dd/mm/yyyy
The field containing the dates is set as StringType()
I am trying to extract the year from the string as follows:
select year(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
I'm using the following the code to extract the quarter
select quarter(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
However, both result in NULL values.
Any thoughts on how to extract the year and quarter from dates with StringType() dd/mm/yyyy?
The table looks like the following:
Could you try the to_date function?
select year(to_date(financials_0_accountsDate, 'dd/MM/yyyy')) from `financiallimited_csv`

How to convert a string datatype column to date format in hive

Could you please guide with below query.
I need to convert below string column to date.
Input and expected output is provided in screenshot.
Input table: column maturity_date is in string datatype.
I tried below but not working as expected
to_date(from_unixtime(unix_timestamp(maturity_date,'MM/DD/YYYY H:mm:ss'),'yyyy-mm-dd')
Try using lower case letters. Upper case means another thing (day of year (D) and week-year (Y)).
to_date(from_unixtime(unix_timestamp(maturity_date,'MM/dd/yyyy H:mm:ss'),'yyyy-MM-dd')
Correct input format is 'MM/dd/yyyy H:mm:ss', not 'MM/DD/YYYY H:mm:ss'
Correct output format is yyyy-MM-dd, not yyyy-mm-dd. mm is minutes. MM is month
Read more about date format used in Hive here SimpleDateFormat

Date Format in pg-node

I have tried to insert date into the postgresql in 'dd.mm.yyyy' format.
First inserted the string type, then the date type
moment(new Date()).format('DD.MM.YYYY') but in database format always like 'yyyy-mm-dd'
What can I do?
Plesase, help

Parsing non iso datetime string to just date part in presto

I have table which stores datetime as varchar
Format looks like this 2018-07-16 15:00:00.0 ,
I want to parse this to extract only date part so that I use date part to compare with date in string format such as '2018-07-20' in where clause. What is the best way to achieve this in presto?
This particular format (based on example value 2018-07-16 15:00:00.0 in the question) is understood by cast from varchar to timestamp. You then need to extract date part with another cast:
presto> SELECT CAST(CAST('2018-07-16 15:00:00.0' AS timestamp) AS date);
_col0
------------
2018-07-16
(1 row)

Resources