Home > Software engineering >  How do I find the first and last day of the previous month using SQL?
How do I find the first and last day of the previous month using SQL?

Time:01-27

I am using IBM Data Studio to access data on an AS400, I cannot figure out how to programmatically find the first and last date of the previous month to use in an BETWEEN clause. For example, if I ran it today it would return 20211201 and 20211231. I have done some other date math in the past but this one is beyond me. Below is an example of a date function I have used successfully in this version of SQL.

{DEC(DATE(DAYS((CURRENT_DATE) - 15 DAY)))}

CodePudding user response:

Try

DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 MONTH

and

DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 DAY
  •  Tags:  
  • Related