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.
Author |
Topic |
cool.mugil
Starting Member
32 Posts |
Posted - 2008-07-31 : 01:10:53
|
hai,i have my table structure as follows,S.No. FromDate ToDate1 01/26/2008 01/26/20082 07/01/2008 07/05/20083 08/09/2008 08/17/2008I will pass two parameters StartDate and EndDate,If the Startdate or Enddate exists in this table,then it should display the count of days.For example,If Startdate=07/02/2008 and Enddate=07/03/2008 then count=2If StartDate=08/01/2008 and Enddate=08/14/2008 then count=6Please guide me.Thanks in advance |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-31 : 01:21:07
|
[code]DECLARE @TABLE TABLE( SNo int, FromDate datetime, ToDate datetime)INSERT INTO @TABLESELECT 1, '20080126', '20080126' UNION ALLSELECT 2, '20080701', '20080705' UNION ALLSELECT 3, '20080809', '20080817'DECLARE @StartDate datetime, @EndDate datetimeSELECT @StartDate = '20080702', @EndDate = '20080703'-- SELECT @StartDate = '20080801',-- @EndDate = '20080814'SELECT DATEDIFF(DAY, CASE WHEN FromDate < @StartDate THEN @StartDate ELSE FromDate END, CASE WHEN ToDate < @EndDate THEN ToDate ELSE @EndDate END) + 1FROM @TABLEWHERE ( FromDate <= @StartDate AND ToDate >= @StartDate )OR ( FromDate <= @EndDate AND ToDate >= @EndDate )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|