Groups | Blog | Home
all groups > sql server programming > march 2007 >

sql server programming : DaysInMonth


Rahul
3/21/2007 11:34:34 PM
Hi,

how we can find out no of days in a particular month.
I am using sql server 2000

Rahul
Uri Dimant
3/22/2007 12:00:00 AM
Hi
1)
select dateadd(month,datediff(month,0,getdate())+1,0)-1
2)
declare @d datetime
set @d=getdate()
select datestring, case
when isdate(datestring/100*100+31) = 1 then 31
when isdate(datestring/100*100+30) = 1 then 30
when isdate(datestring/100*100+29) = 1 then 29
when isdate(datestring/100*100+28) = 1 then 28
end
from (
select convert(varchar,@d,112) as datestring
) D

3)
declare @d datetime
set @d = getdate()
select 32-day(@d-day(@d)+32)







[quoted text, click to view]

Ramesh Subramaniyan
3/22/2007 12:13:11 AM

select datediff(day,'2/1/2006','3/1/2006') try this
previous month and next moth
[quoted text, click to view]
Mark Siltala
3/23/2007 9:44:03 AM
OK, try this:

Create proc p_GetMonthDays (@year varchar(4),@month varchar(2))
as
declare @date char(10)
set @date = @month + '-01-' + @year
select datepart(day,dateadd(day,-1,(dateadd(month, 1, @date)))) 'Days In
Month'

You need to pass the year (2008) and month number (2),

exec p_GetMonthDays 2008,2

returns this:

Days In Month
-------------
29

(1 row(s) affected)

Of course, this could be 'neatened' up a lot (enter month name, convert to
month number, remove column name, No error checking, etc.), but it's a
start.

It starts with the first day of the month you enter, adds one month, goes
back one day, and gets that day number.

Mark Siltala



[quoted text, click to view]

AddThis Social Bookmark Button