Convert string to date format

 If you have date in string datatype like '20230301' and  you want to convert it into 2023-03-01 in data datatype then please find below examples.


Solution 1

select process_date, to_date(process_date, 'yyyyMMdd') as new_date from table

Now you will below results:

process_date new_date
20230603         2023-06-03



Solution 2


If you want to get some prior date on base of exiting date and you date in string datatype
'YYYYMMDD' e.g 20230603         


select process_date, ,date_add(to_date(process_date, 'yyyyMMdd'),-1) as new_date
from table


Results:

process_date new_date
20230603         2023-06-02

 

Comments

Popular posts from this blog

Date format issue with spark sql

Hive Partition sub folders HIVE_UNION_SUBDIR_1,HIVE_UNION_SUBDIR_2,HIVE_UNION_SUBDIR_8

Dataiku and Dremio date difference