How to get the date of the week before the current date in DB2

Source: Internet
Author: User

SELECT current_date-(DAYOFWEEK (current_date)-2 + (Row_number () over (ORDER in 1))) Day as Resultfrom SYSIBM. Syscolumnsfetch first 7 rows only; output: 2013-03-24 2013-03-23 2013-03-22 2013-03-21 2013-03-20 2013-03-19 2013-03-18 You can see the sentiment The situation, you do a bit of sorting. Cross-examine the minor changes can find out what you said the result is only you this method is to check out all the dates before last week, and then take the first 7, so how to sort it also please advise! Answer Alas, originally I want to lazy ... It seems to be to write their own ah ... SELECT current_date-(DAYOFWEEK (current_date)-1) DAY-7 day+ (Row_number () over (ORDER in 1)) Day as Resultfrom Sysib M.syscolumnsfetch first 7 rows only; query result: 2013-03-18 2013-03-19 2013-03-20 2013-03-21 2013-03-22 2013-03-23 2013-03-24 Note: D Ayofweek is the day of the current week, which is the query. (Sunday is the first day) Current_date-(DAYOFWEEK (current_date)-1) Day is getting the first days of the week (i.e. Sunday March 24) current_date-(DAYOFWEEK (current_date) -1) DAY-7 Day is getting last week's first days (i.e. last week's Sunday March 17) current_date-(DAYOFWEEK (current_date)-1) DAY-7 day+ (row_number () OVE R (ORDER by 1)) Day is starting from last Sunday, each row of data + 1 days. That is, from March 18 is the 1th row, March 19 is the 2nd row fetch first 7 rows only; is only 7 rows are retrieved.

How to get the date of the week before the current date in DB2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.