Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I need to get the name and number of days between two dates. is there a function or a way in SQL to do that.

example: how many sundays, mondays,....., saturdays are in between 15 june 2012 and 30th june 2012.

Thnx
Posted

There is no built in function to do that, you will have to write your own. I suggest you start by looking at this: http://forums.asp.net/t/1439809.aspx/1[^]
And this: http://stackoverflow.com/questions/8642870/calculate-all-sundays-mondays-saturdays-between-two-days-in-sql-server[^]
 
Share this answer
 
The DATEDIFF function[^] should be sufficient for your requirement.

E.g.
select DATEDIFF(DAY, @date1,@date2)
 
Share this answer
 
Hi ,
Check this
SQL
declare @t1 nvarchar(10)
set @t1 ='1/1/2010'
declare @t2 nvarchar(10)
set @t2 ='1/3/2010'

select datediff(DAY,convert(datetime, convert(char(10), @t1,110)),convert(datetime, convert(char(10),@t2, 110)))

Best Regards
M.Mitwalli
 
Share this answer
 
read this Find all SQL Server DateTime Formats[^]
i got it ur question answer. please look this Select dates of a day between two dates.[^] article it will fulfill your requirement.
if it will solve your problem than click accept solution.
look this simple example:-

SQL
declare @dt1 datetime set @dt1 ='15/jun/2012'
declare @dt2 datetime set @dt2 ='30/jun/2012'
declare @cnt int
declare @DayName varchar(50)
declare @i int set @i=1
set @cnt=datediff(day,@dt1,@dt2)
print @cnt

while (@i<=@cnt)
begin
set @DayName=''
set @dt2=dateadd(day,@i,@dt1)
set @DayName= datename(dw,@dt2)
print @DayName -- now add your logic for count daysname (sunday, monday etc.) 
set @i=@i+1
end
 
Share this answer
 
v3
Comments
Prosan 20-Jun-12 4:50am    
look second article Select dates of a day between two dates or my example i think after that your problem will solved

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