0

I want to filter data from SQL Server 2008 R2 using FromDate and ToDate with another 2 user input dates. I want query for that.

I want to filter data such a way that If user input 2 dates or any date in between of them lies in FromDate or 'ToDate' or in between date then it filters data.

For i.e.

 FromDate             ToDate
15-11-2014          20-11-2014

if user input dates are 11-11-2014 and 20-12-2014. It means in between dates of this 2 dates lies in between FromDate and ToDate so it should return this record.

Now, if user input dates are 11-11-2014 and 14-11-2014. It means in between dates of this 2 dates does not lie in between FromDate and ToDate so it should not return this record.

It must match given dates and as well as in between dates.

3 Answers 3

1

you Can use this :

Where

DATEADD(dd, 0, DATEDIFF(dd, 0, FromDate)) > '15-11-2014'

and

DATEADD(dd, 0, DATEDIFF(dd, 0, ToDate)) < '20-11-2014'

Sign up to request clarification or add additional context in comments.

Comments

0

please check the example, you just compare the parameter with both datetime column to extract the rows.

declare @table table(id int,frdt datetime, todt datetime)

insert into @table values (1,GETDATE()-20, GETDATE()-19)
,(1,GETDATE()-9, GETDATE()-8)
,(1,GETDATE()+20, GETDATE()+18)
,(1,GETDATE(), GETDATE()-1)
,(1,GETDATE()-20, GETDATE())
,(1,GETDATE()-10, GETDATE()+10 )

select * from @table

declare @frdt datetime = null , @todt datetime = getdate()-10

select @frdt, @todt,* from @table
where
 (@frdt is null or @frdt between frdt and todt)
and
 (@todt is null or @todt between frdt and todt)

 select @frdt = GETDATE() , @todt = GETDATE()

 select @frdt, @todt,* from @table
where
 (@frdt is null or @frdt between frdt and todt)
 and 
 (@todt is null or @todt between frdt and todt)

2 Comments

where you got the answer for this?
@MONEYMSN, I don't remember, but what do you want from me or us? Have any question or query, then post your question or comment here with example.
0

From what I understand from your question.

select 'a' A, CONVERT(date,'11/15/2014') FromDate, CONVERT(date, '11/20/2014') ToDate into #Temp

declare @frmDate date; 
declare @toDate date;

set @frmDate = Convert(date, '11/11/2014');
set @toDate = CONVERT(Date, '12/20/2014');

select * from #Temp where FromDate>=@frmDate and ToDate<=@toDate

set @frmDate = Convert(date, '11/11/2014');
set @toDate = CONVERT(Date, '11/14/2014');

select * from #Temp where FromDate>=@frmDate and ToDate<=@toDate

Hope fully it help you :)

4 Comments

this is not the thing im looking for. please read my post properly.
Are you expecting 1 input or 2 from user? @CSAT.
think that there is one row with id and 2 dates. and i want to compare it based on user input dates. and it should fulfill requirements as per details in post.
I still don't get it. Sorry @CSAT :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.