Find last sunday

前端 未结 10 1315
余生分开走
余生分开走 2020-12-05 20:40

How will you find last sunday of a month in sql 2000?

10条回答
  •  醉话见心
    2020-12-05 21:02

    An alternative approach, borrowed from data warehousing practice. Create a date-dimension table and pre-load it for 10 years, or so.

    TABLE dimDate (DateKey, FullDate, Day, Month, Year, DayOfWeek, 
                   DayInEpoch, MonthName, LastDayInMonthIndicator, many more..)
    

    The easiest way to fill-in the dimDate is to spend an afternoon with Excel and then import to DB from there. A half-decent dimDate table has 50+ columns -- anything you ever wanted to know about a date.

    With this in place, the question becomes something like:

    SELECT max(FullDate)
    FROM dimDate
    WHERE DayOfWeek = 'Sunday'
          AND Month = 11
          AND Year = 2009;
    

    Essentially, all date related queries become simpler.

提交回复
热议问题