Home > database >  So there are 2 date columns in a table In sql
So there are 2 date columns in a table In sql

Time:05-25

I have table , with date format in YYYY-MM-DD.

Acc_No On_board_date Arrival_date
001 2022-02-01 2022-02-04
002 2022-03-10 2022-03-07
003 2022-03-12 2022-03-25

What I want is Acc_No's whose arrival date is /- 5 days from on_board_date which will result in like these

Acc_No On_board_date Arrival_date
001 2022-02-01 2022-02-04
002 2022-03-10 2022-03-07

This is what I have tried

select * from table
where on_board_date <= dateadd(arrival_date,5)
or on_board_date <= datesub(arrival_date,5)

Not getting desired result with this

CodePudding user response:

You can try this :

select * from table
where on_board_date <= arrival_date   INTERVAL '5' day

CodePudding user response:

I tried the one Akina suggested

WHERE DATEDIFF(On_board_date, Arrival_date) BETWEEN -5 AND 5

This worked.

CodePudding user response:

What I want is Acc_No's whose arrival date is /- 5 days from on_board_date

select * 
from test
where On_board_date  <= (Arrival_date   INTERVAL 5 day) 
or On_board_date  >= (Arrival_date - INTERVAL 5 day) ;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0ab34cf269d183e95f20395c940a650b

  • Related