By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,377 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,377 IT Pros & Developers. It's quick & easy.

Get number of days in a month

rsrinivasan
100+
P: 221
Hi all,

Is SQLSERVER have any built in function to get number of days for a given month?

Thanks,
May 22 '08 #1
Share this Question
Share on Google+
2 Replies


deepuv04
Expert 100+
P: 227
Hi all,

Is SQLSERVER have any built in function to get number of days for a given month?

Thanks,
use the following query to get number of days in a given month
Expand|Select|Wrap|Line Numbers
  1. declare @Date datetime
  2. select @Date = Getdate()
  3.  
  4. select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(@Date) as varchar)+
  5.         '-'+cast(month(@Date) as varchar)+'-01' as datetime))))
  6.  
my logic is i am taking the year and month of the given date and adding 1 to the month, then append 01 at the end so that we are moving to the 1 st of the next month. from the result i am moving one day back which is last day of the given month. if we take the day part from the final value which is number of days in a given month
May 22 '08 #2

ck9663
Expert 2.5K+
P: 2,878
Here's another one:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @dtDate DATETIME
  2.     SET @dtDate = getdate()
  3. SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
  4.  
-- CK
May 22 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.