Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date range select

Author  Topic 

bnd_ck
Starting Member

4 Posts

Posted - 2014-07-15 : 08:35:49
I have a table has following fields(Process, datestart,dateend,datedue,,,,)
Process datestart dateend ...
A 14.07.2017 20.07.2014
B 15.07.2014 21.07.2014
C 24.07.2014 30.07.2014
what i want when user enters from date and end date. i want to filter records which are fallen between those two input dates.
if user enters 17.07.2014 and 20.07.2014 as input range. query should show only A and B process as
A 14.07.2017 20.07.2014
B 15.07.2014 21.07.2014
qiuck help plzz

MichaelJSQL
Constraint Violating Yak Guru

252 Posts

Posted - 2014-07-15 : 09:41:01
CREATE TABLE #Dates
(
ID INT identity(1,1),
Process varchar(5),
datestart date,
dateend date
)

INSERT INTO #Dates
VALUES('A','07/14/2014','07/20/2014'),('B','07/15/2014','07/21/2014'),('C','07/24/2014','07/30/2014')

DECLARE @start date = '7/15/2014'
DECLARE @end date = '7/20/2014'

-- any that falls in range -- i think this is what you are describing. this is either the end or the start is occurring between the periods entered
SELECT * FROM #Dates
WHERE @start between datestart AND dateend
AND @end between datestart AND dateend
Go to Top of Page

bnd_ck
Starting Member

4 Posts

Posted - 2014-07-15 : 10:02:45
thanks
you mean stored procedure ?
how about where clause in sql for this situation? if user enters random 2 dates as input.
Go to Top of Page

MichaelJSQL
Constraint Violating Yak Guru

252 Posts

Posted - 2014-07-15 : 17:36:15
You can turn that into a procedure . You would probably want to protect against end date being before start date. you could also so tighten up the date logic so that the dates are exact or that the start date is in the range, but the end date is firm: meaning nothing that ends after the parameter regardless if it starts in the range - it all depends on what you are trying to do.
Go to Top of Page
   

- Advertisement -