How do I get the last day on the month using SQL Reporting Services

后端 未结 7 969
花落未央
花落未央 2020-12-13 04:44

In SQL Server Reporting Services, how would I calculate the last day of the current month?

相关标签:
7条回答
  • 2020-12-13 05:26

    Here is the answer I came up with

    =DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
    
    0 讨论(0)
  • 2020-12-13 05:28

    you can use an assembly for doing this work by adding it as a reference.

    0 讨论(0)
  • 2020-12-13 05:32

    From the blog of a Microsoft SQL Team member:

    -- returns the last day of the current month.
    select dbo.Date(year(getdate()), month(getdate())+1,0)
    

    http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx

    Hope this helps! --Dubs

    0 讨论(0)
  • 2020-12-13 05:34

    There is an even easier way as in the marked answer:

    =DateSerial(Year(Now()), Month(Now())+1, 0)

    Since DateSerial() accepts integers as parameters one does not have to use AddMonths() and AddDays(). As in the example an instant calculation inside DateSerial() is possible.

    Furthermore day 1 is the first day of the month while the first day minus one day is the last day of the month before (1-1=0). So the example will return the date of the last day of the current month.

    0 讨论(0)
  • 2020-12-13 05:45

    I know you've found your own answer, but I'd suggest this alternative:

    =Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
    

    It's a little easier to read, in my opinion, and might have slightly better performance (though most likely unnoticeable)

    And, of course, if you wanted to pad out that date to 23:59:59, as is often necessary, just modify slightly:

    =Today.AddDays(1-Today.Day).AddMonths(1).AddSeconds(-1)
    
    0 讨论(0)
  • 2020-12-13 05:47

    You may try this expression, Just replace now with your date field.

    =DateSerial(Year(Now), Month(Now), 1)

    Hope this helps.

    Regards

    0 讨论(0)
提交回复
热议问题