I want to get number of days in a particular month in sql server 2005 [Resolved]

Posted by Amritha444 under Sql Server on 6/20/2011 | Points: 10 | Views : 23939 | Status : [Member] | Replies : 5
Hi All

I want to get number of days in a particular month in sql server 2005 .Actually i want to prepare salary for employees in particular month..i have month and year
how to get count of days in a particular month using month and year

Thanks In Advance




Responses

Posted by: Ndebata on: 6/20/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi
You can get it like this
Try to get the first day of that month , then add one month , then minus one day , and get the day of that result.
your query will be something like
Declare @MyDate datetime

set @MyDate=getdate()
Select DAY(DATEADD(DD,-1,DATEADD(MM ,1,DATEADD(DD, 1 - DAY(@MyDate), @MyDate))))

Thanks,
Debata

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prakash_vijay on: 6/20/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Amritha ,

Please refer to following link for solve yopur problem :

http://www.sql-server-helper.com/functions/get-days-in-month.aspx

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/20/2011 [Member] Starter | Points: 25

Up
0
Down
try this

select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))))


Thanks&Regards
LakshmiNarayana Nalluri.

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vilasgite080 on: 6/20/2011 [Member] Starter | Points: 25

Up
0
Down

hi for preparation of Salary..please check this

http://msdn.microsoft.com/en-us/library/ms189794.aspx
--------------------------------------------------------------------------------------------------
MARK AS ANSWER! if it helps you...

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vilasgite080 on: 6/20/2011 [Member] Starter | Points: 25

Up
0
Down

DECLARE @counter INT
DECLARE @days INT
DECLARE @date DATETIME
DECLARE @yearTable TABLE (monthName VARCHAR(20), numDays INT)
BEGIN
SET @counter = 1
WHILE (@counter < 13)
BEGIN
SET @date = CONVERT(DATETIME, STR(@counter) + '/01/2003')
SELECT @days = DAY(DATEADD (m, 1, DATEADD (d, 1 - DAY(@date), @date)) - 1)

INSERT INTO @yearTable VALUES (DATENAME (month, @date), @days)
SET @counter = @counter + 1
END
SELECT * FROM @yearTable
END
----------------------
http://www32.brinkster.com/srisamp/sqlArticles/article_5.htm
--------------------------
MARK AS ANSWER!........if it helps you.


Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response