• KGJ-Dev - Friday, January 19, 2018 9:16 AM

    Hi,

    I need to get Year, monthname in three letters, startdate and enddate  of the month for last two years based on today’s date. Any sample query please

    You should find it easy enough from here:

    SELECT bom = DATEADD(day,1,DATEADD(month,-1,x.eom)), x.eom

    FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),

    (13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)) d (n)

    CROSS APPLY (SELECT eom = DATEADD(MONTH,-n,EOMONTH(GETDATE()))) x

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden