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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get number of days within a date range

Author  Topic 

prasantapaul
Starting Member

6 Posts

Posted - 2014-03-12 : 08:39:16
I have few date ranges in my database table like below.

01-Jan-2014 to 30-Mar-2014
31-Mar-2014 to 31-Mar-2014

Now I have given a date range (say 26-Feb-2014 to 04-Apr-2014) to retrieve the number of days

which exists in my database date range.

Can you please help me out in this regard?

Muj9
Yak Posting Veteran

75 Posts

Posted - 2014-03-12 : 10:11:41
if you can provide more information on your table are date range and number of dates in the same table or different tables and what sort of out put are you expecting give example.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2014-03-12 : 10:18:04
you might want to take a look at DateDiff function, considering that you've two date fields

e.g.
Declare @date1 datetime,@date2 datetime
set @date1 = '2014-03-01'
set @date2 = '2014-03-12'
select DateDiff (dd,@date1,@date2)

Cheers
MIK
Go to Top of Page

prasantapaul
Starting Member

6 Posts

Posted - 2014-03-13 : 05:55:06
Hi Mik

Thanks for your response. But this is not correct. Because I want to get the number days from two date ranges.

Thanks
quote:
Originally posted by MIK_2008

you might want to take a look at DateDiff function, considering that you've two date fields

e.g.
Declare @date1 datetime,@date2 datetime
set @date1 = '2014-03-01'
set @date2 = '2014-03-12'
select DateDiff (dd,@date1,@date2)

Cheers
MIK



Thanks & Regards,
P Paul
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2014-03-13 : 08:24:13
I just gave you an example as how you can get number of days when you've two dates .. range is always meant to be a two date points.

Would you mind explaining why this is not correct? What data you have and what is your desired output for that. It would be better if you provide sample data in the form of insert statement so that we can utilize it. e.g. insert into @tableName values (.....)

Cheers
MIK
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2014-03-13 : 21:02:15
What do you mean by two ranges? The MIK solution is correct. the date range means should be two dates.

elaborate your table and column details.
Go to Top of Page
   

- Advertisement -