Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Between Two Date Condition in Sql Server

I have table called Master and i need all the possible records of all matching date as per user Input between @FromDate and @Todate....

Master Table:

ID    FromDate    ToDate 
1    18/03/2014   18/03/2014
2    01/03/2014   05/03/2014
3    06/03/2014   08/03/2014  

My Query:

select ID 
from master M 
where M.FromDate between('10/03/2014') and ('17/03/2014') 
    or M.ToDate between ('10/03/2014') and ('17/03/2014')

User Input:

FromDate      ToDate
01/03/2014   06/03/2014
01/03/2014   31/03/2014
09/03/2014   09/03/2014

Expected Output:

2,3
1,2,3
No Records
like image 812
Pradeep Avatar asked Dec 21 '25 15:12

Pradeep


1 Answers

I Got The Solution:

SELECT ID 
FROM master M 
WHERE ((M.FromDate BETWEEN '10/03/2014' AND '17/03/2014') 
     OR (M.ToDate BETWEEN '10/03/2014' AND '17/03/2014'))

parenthesis is Very Important in Sequel Server.....

like image 51
Pradeep Avatar answered Dec 24 '25 06:12

Pradeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!