Home Forums SQL Server 2012 SQL 2012 - General SQL help - I need the last day for the last month in YYYYMMDD format RE: SQL help - I need the last day for the last month in YYYYMMDD format

  • Shorter version without the need of scalar functions.

    DECLARE @Date SMALLDATETIME = GETDATE();

    SELECT CONVERT( char(8), DATEADD(MM, DATEDIFF(MM, 0, @Date), -1), 112);

    More formulas: http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2