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
 Condition in Two Date field with Two Date Paramete

Author  Topic 

vinaym
Starting Member

8 Posts

Posted - 2011-10-03 : 09:05:48
I have an ID field two date fields in table named as From_Date and To_Date. I want to select this dates with date criteria.

ID From_Date To_Date
1 2011-05-03 2011-05-28
2 2011-04-29 2011-05-10
3 2011-05-09 2011-05-20
4 2011-04-28 2011-05-09
5 2011-04-29 2011-05-19
6 2011-05-09 2011-05-09
7 2011-05-09 2011-05-09
8 2011-05-09 2011-05-09
9 2011-05-21 2011-05-21
10 2011-06-06 2011-06-06
11 2011-05-21 2011-05-28
12 2011-05-14 2011-05-28
13 2011-05-23 2011-05-24
14 2011-05-27 2011-05-27
15 2011-05-23 2011-05-24
Now I want two parameter for condition of start date and end date like

@StartDate = '05/01/2011'
@EndDate = '05/10/2011'
And I want Output from these parameters when ever array of dates between startdate and enddate parameter should be compare to array of dates between From_Date and To_Date field

Output:

ID From_Date To_Date
1 2011-05-03 2011-05-18
2 2011-04-29 2011-05-10
3 2011-05-09 2011-05-20
4 2011-04-28 2011-05-09
5 2011-04-29 2011-05-19
6 2011-05-09 2011-05-09
7 2011-05-09 2011-05-09
8 2011-05-09 2011-05-09
is this possible in single query instead use of trigger or function?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-03 : 09:09:49
[code]
SELECT ID,From_date,To_date
FROM Table
WHERE From_Date >=@StartDate AND To_Date<=@EndDate
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vinaym
Starting Member

8 Posts

Posted - 2011-10-03 : 09:25:03
But using this simple condition couldn't get all related rows.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-10-03 : 09:26:29
SELECT ID, From_date, To_date
FROM Table
WHERE From_Date <= @EndDate AND To_Date>= @StartDate



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -