.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Count No of days between two dates in SQL

Posted By:abhays       Posted Date: April 30, 2014    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

below query explains how to count No of days between two dates in SQL
 

Declare @startdate datetime
Declare @enddate datetime
Declare @count int
Declare @NameOfDay varchar(15)

Declare @day int
set @count=0
set @startdate='2013-10-01'
set @enddate='2013-10-30'
Set @NameOfDay='Tuesday'

 if(@NameOfDay='Sunday')
 Begin
 set @day=1
 End
 Else  if(@NameOfDay='Monday')
 Begin
 set @day=2
 End
 
 Else  if(@NameOfDay='Tuesday')
 Begin
 set @day=3
 End
 Else  if(@NameOfDay='Wednesday')
 Begin
 set @day=4
 End
 Else  if(@NameOfDay='Thursday')
 Begin
 set @day=5
 End
 Else  if(@NameOfDay='Friday')
 Begin
 set @day=6
 End
 Else  if(@NameOfDay='Saturday')
 Begin
 set @day=7
 End
    while @startdate<=@enddate
    Begin
        IF DatePart(WEEKDAY,@startdate)= @day
            SET @count=@count+1
        SET @startdate=DateAdd(d,1,@startdate)
    END
select @count

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend