Posts

Showing posts from June, 2023

Extract Previous Month Date in SQL

  If you are working  on Spark SQL and you want to extract previous month last date from existing date  and unfortunately your present date in  text format e.g '20230301' Problem : You have column called "process_date" which contain date like '20230310' (March) and in same table you you have another date in same column have date like '20230228' (Feb) which is the last date of previous month.   Existing date -  '20230310'   Requirement :- To extract '20230228' (Last date of previous month) select     process_date   from table1    where  process_date =                   (                          select max(process_date)                                            ...

Date of previous month

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