Find last sunday

2019-01-14 17:27发布

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

9条回答
贼婆χ
2楼-- · 2019-01-14 17:57

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-14 18:08
select dateadd(day,1-datepart(dw, getdate()), getdate())
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-14 18:08
select next_day(last_day(sysdate)-7, 'Sunday') from dual
查看更多
登录 后发表回答