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
 General SQL Server Forums
 New to SQL Server Programming
 Days in a month

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2007-12-21 : 13:53:52
So Im trying to find the number of days in a certain month I know how I want to do but cant figure out how to code it

i want to take the first date of the getadate() and then ad a month then subtract 1 day and ill have the number of days in a month how would i code that?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-21 : 14:00:51
Try this:-

DATEDIFF(d,CONVERT(datetime,'01/'+@Month + @year),DATEADD(mm,1,CONVERT(datetime,'01/'+@Month + @year)))

where @Month & @year are your interested month & year
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2007-12-21 : 14:54:07
maybe
DATEDIFF(d,CONVERT(datetime,@year+@month+'01'),DATEADD(mm,1,CONVERT(datetime,@year+@month+'01')))


[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-22 : 02:21:37
[code]select datepart(day, (dateadd(month, datediff(month, 0, getdate()) + 1, -1)))[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-22 : 04:28:22
SELECT DAY(DATEADD(MONTH, DATEDIFF(MONTH, '18991231', @theDate), '18991231'))


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-24 : 01:26:43
quote:
Originally posted by gavakie

So Im trying to find the number of days in a certain month I know how I want to do but cant figure out how to code it

i want to take the first date of the getadate() and then ad a month then subtract 1 day and ill have the number of days in a month how would i code that?


Do you want to find it from getdate() or the month value you pass?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -