Function to get number of days in month

  • And why not:

    Declare @day DATETIME

    Set @day='2011-02-15'

    select datediff(day,dateadd(d,1-day(@day),@day),dateadd(m,1,dateadd(d,1-day(@day),@day)))

    🙂

  • ramanamreddy (5/2/2011)


    how to give any mnth to the above query?

    Apologies for the late reply:

    SELECT DISTINCT d.DaysInCalendarMonth

    FROM dbo.dwCalendars d

    WHERE d.CalendarYear = 2011

    AND d.CalendarMonthName = 'February'

  • This one doesn't work unless the @testdate is the 1st day of the month. E.g. with @testdate = '2011-01-30' it incorrectly returns 29.

  • EITCL (6/20/2011)


    This one doesn't work unless the @testdate is the 1st day of the month. E.g. with @testdate = '2011-01-30' it incorrectly returns 29.

    There are several things wrong with the information in the article.

    There are many posts in the discussion thread that show better ways of converting a date.

  • If you change the last bit of the case to:

    else case when ((Year(@day) % 4 = 0) and ((Year(@day) % 100 != 0) or (Year(@day) % 400 = 0)))

    you will perform fewer tests.

    With ((A and B) or C), both A and C have to be evaluated even when A is false. When changed to (A and (B or C)), B and C are only evaluated when A is true.

  • Select Day(EOMONTH('2016-06-11 15:13:04.497')) AS NoOfDays

    NoOfDays

    -----------

    30

    select day('2016-06-11 15:13:04.497') as daystillnow

    daystillnow

    -----------

    11

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply