Return just the last day of each month with SQL

前端 未结 9 831
执笔经年
执笔经年 2020-12-30 05:22

I have a table that contains multiple records for each day of the month, over a number of years. Can someone help me out in writing a query that will only return the last d

9条回答
  •  旧巷少年郎
    2020-12-30 05:50

    Use the EOMONTH() function if it's available to you (E.g. SQL Server). It returns the last date in a month given a date.

    select distinct
    Date 
    from DateTable
    Where Date = EOMONTH(Date)
    

    Or, you can use some date math.

    select distinct
    Date
    from DateTable
    where Date = DATEADD(MONTH, DATEDIFF(MONTH, -1, Date)-1, -1)
    

提交回复
热议问题