Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two dates with Oracle SQL if it's same date by only day and month

I have saved a date in my oracle db. (Pattern: dd.MM.yyyy) Now I have to check/compare by day and month, if today ss the same date or exactly 6 months after. But the year has to be ignored. For example. My date is 01.02.2001. Then it has to be "true" at the 01.02.2002.

Also I need to check if the date + 6 months is true. (halfyearly). (For example 01.02.2002 --> 01.08.2002)

I tried with sysdate, but I'm a noob in sql.

like image 284
Christian Avatar asked Dec 12 '22 07:12

Christian


2 Answers

You can do the comparison using to_char():

where to_char(yourdate, 'MM-DD') = to_char(sysdate, 'MM-DD')

For six months:

where to_char(yourdate, 'MM-DD') = to_char(add_months(sysdate, 6), 'MM-DD')
like image 168
Gordon Linoff Avatar answered May 16 '23 03:05

Gordon Linoff


You can also use

substr(date1,5) =substr(date2,5)

this code compares first five characters of the data and checks if they are identical or not.

like image 44
MR AND Avatar answered May 16 '23 03:05

MR AND