How to get Number of days of current Month in sql server?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ankur JainRank #2,475 · 567 reputationPosted Apr 20, 2014, 8:50 PM EDT
CREATE FUNCTION [dbo].[ufn_GetDaysInMonth] ( @pDate DATETIME ) RETURNS INT AS BEGIN RETURN CASE WHEN MONTH(@pDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31 WHEN MONTH(@pDate) IN (4, 6, 9, 11) THEN 30 ELSE CASE WHEN (YEAR(@pDate) % 4 = 0 AND YEAR(@pDate) % 100 != 0) OR (YEAR(@pDate) % 400 = 0) THEN 29 ELSE 28 END END END GOAbhay ShankerRank #375 · 6.7K reputationPosted Apr 20, 2014, 9:16 PM EDT
set @dt = getdate()
select
DT = @dt,
[Days in Month] =day(dateadd(mm,datediff(mm,-1,@dt),-1))