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.
| Author |
Topic |
|
WalkerDA
Yak Posting Veteran
61 Posts |
Posted - 2004-09-01 : 11:47:44
|
| Any idea of how I can code a proc. to know how many days are in a month without telling it?Derrick |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-01 : 12:11:26
|
| i don't know any sql function for that, but this shoul do it:declare @Month intset @Month = 5declare @NumOfDayes table (monthNum int, dayNum int)insert @NumOfDayesselect 1, 31 union allselect 2, (select case when (year(getdate())%4 = 0) or (year(getdate())%100 = 0) and (year(getdate())%400 = 0) then 29else 28end) union all --this fixes leap yearsselect 3, 31 union allselect 4, 30 union allselect 5, 31 union allselect 6, 30 union allselect 7, 31 union allselect 8, 31 union allselect 9, 30 union allselect 10, 31 union allselect 11, 30 union allselect 12, 31select dayNumfrom @NumOfDayeswhere MonthNum = @MonthGo with the flow & have fun! Else fight the flow :) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-01 : 12:15:07
|
| [code]USE NorthwindGOCREATE FUNCTION udf_LastDay (@x datetime)RETURNS datetime BEGIN DECLARE @y datetime SELECT @y = DATEADD(d,-1,CONVERT(char(2),MONTH(DATEADD(mm,1,@x)))+'/01/'+CONVERT(char(4),YEAR(@x))) RETURN @y ENDGOSELECT dbo.udf_LastDay(GetDate())GO[/code]But I thought we had found a simpler way....Brett8-) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-01 : 12:33:48
|
or even simpler declare @date datetimeset @date = getdate()select day(dateadd(m, 1, dateadd(d, -day(@date), @date)))Go with the flow & have fun! Else fight the flow :) |
 |
|
|
xptm
Starting Member
7 Posts |
Posted - 2004-09-01 : 12:40:55
|
| Look here:[url]http://www.informit.com/articles/article.asp?p=30939&seqNum=9[/url] |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-01 : 15:49:34
|
quote: Originally posted by WalkerDA Any idea of how I can code a proc. to know how many days are in a month without telling it?Derrick
What do you mean by, "Not telling it"?Brett8-) |
 |
|
|
WalkerDA
Yak Posting Veteran
61 Posts |
Posted - 2004-09-01 : 19:11:56
|
| Sorry guys been in meetings all day... By not telling it would be by using functions as in spirit1's example or even the udf you designed.This has been one of those days..thanks!Derrick |
 |
|
|
|
|
|
|
|