Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Please help me
my master table is like

Id Name Fromdate Todate
1 SEASON1 2014-01-01 2014-04-30
2 SEASON2 2014-05-01 2014-08-31
3 SEASON3 2014-09-01 2014-12-31

and I got two dates as 2014-15-01 and 2014-12-20 when am passing these two date I need three
set of records aginst each season as

Name Days
Season1 106
Season2 123
Season3 111


thanks in advance
Posted
Updated 1-Aug-14 2:04am
v2
Comments
CHill60 31-Jul-14 8:37am    
Not clear ... where do the 40 and 228 come from?
victowork 1-Aug-14 0:08am    
40 is the no of days between 21-jan-2014 to 30-april-2014 and 228 is the no of days between
1-may-2014 to 15-dec-2014
Maciej Los 1-Aug-14 1:59am    
Not true!
between 2014-01-21 and 2014-04-30 is 99 days difference (11 days of January, entire: February, March and April)
between 2014-05-01 and 2014-12-15 is 228 days difference
Maciej Los 1-Aug-14 2:10am    
Provide sample data and the structure of table. Use "Improve question" widget.

 
Share this answer
 
Comments
victowork 1-Aug-14 0:10am    
how can i get the days 40 is the no of days between 21-jan-2014 to 30-april-2014 and 228 is the no of days between
1-may-2014 to 15-dec-2014 using datediff
ZurdoDev 1-Aug-14 7:17am    
1. 40 is not the number of days between 2014-01-21 and 2014-04-30.
2. Did you look at how to use datediff? If you want the number of days between 2 dates, use DateDiff
Try this logic ,it works

SQL
declare @fdate datetime = '01/15/2014'
declare @tdate datetime = '12/20/2014'

select Name, DATEDIFF(DAY, CASE WHEN (FromDate >  @fdate) THEN FromDate ELSE  @fdate End , CASE WHEN (ToDate  <  @tdate) THEN ToDate ELSE  @tdate End) AS 'Days'    FROM #data
 
Share this answer
 
Try this:
SQL
DECLARE @Seasons TABLE(SeasonID INT IDENTITY(1,1), SeasonName VARCHAR(30), FromDate DATETIME, ToDate DATETIME)


INSERT INTO @Seasons(SeasonName, FromDate, ToDate)
VALUES('SEASON1', '2014-01-01', '2014-04-30'),
('SEASON2', '2014-05-01', '2014-08-31'),
('SEASON3', '2014-09-01', '2014-12-31'),
('SEASON4', '2015-01-01', '2015-04-30'),
('SEASON5', '2015-05-01', '2015-08-31'),
('SEASON6', '2015-09-01', '2015-12-31')

SELECT *
FROM @Seasons 

DECLARE @sDate DATETIME
DECLARE @eDate DATETIME

SET @sDate = '2014-01-15'
SET @eDate = '2014-12-20'

SELECT SeasonID, SeasonName, FromDate, ToDate, startDate, endDate, DATEDIFF(DAY,startDate,endDate) + 1 AS NoOfDays 
FROM (
	SELECT SeasonID, SeasonName, FromDate, ToDate,
		startDate = CASE
						WHEN FromDate<@sDate THEN @sDate
						WHEN FromDate>=@sDate THEN FromDate
					END,
		endDate = CASE
					WHEN ToDate <= @eDate  THEN ToDate 
					WHEN ToDate>@eDate THEN @eDate
				END
	FROM @Seasons 
) AS T
WHERE DATEDIFF(DAY,startDate,endDate)>0
 
Share this answer
 
Comments
victowork 4-Aug-14 4:15am    
thanks man works great
Maciej Los 4-Aug-14 4:30am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900