SQL about dates, which can be useful when making reports!!!

Source: Internet
Author: User




--Get year, day, quarter

Select
To_char (date, ' YYYYMMDD ') Date_id,to_char (date, ' yyyy ') | | Years ' | | To_char (date, ' mm ') | | Month ' | | To_char (date, ' dd ') | | Day ' Date_name,
To_char (date, ' Yyyymm ') Month_id,to_char (date, ' yyyy ') | | Years ' | | To_char (date, ' mm ') | | Month ' Month_name,
' Q ' | | To_char (date, ' q.yyyy ') Quarterid,to_char (date, ' yyyy ') | | The First ' | | To_char (date, ' Q ') | | Quarter ' quarterid_name,
To_char (date, ' yyyy ') year_id,to_char (date, ' yyyy ') | | Year ' Year_name
From
Select To_date (' 2000-01-01 ', ' yyyy-mm-dd ') + (rownum-1) date from user_objects where rownum<367 and To_date (' 2000-01-01 ') , ' yyyy-mm-dd ') + (rownum-1) <to_date (' 2001-01-01 ', ' yyyy-mm-dd ')
);





--Get quarterly and monthly correspondence

SELECT DISTINCT to_char (date, ' Q ') quarter, To_char (to_date (' 2001-01-01 ', ' yyyy-mm-dd ') + (rownum-1), ' yyyymm ') date from (
Select To_date (' 2001-01 ', ' yyyy-mm ') + (rownum-1) date from user_objects where rownum<367 and To_date (' 2001-01-01 ', ' Yyyy-mm-dd ') + (rownum-1) <to_date (' 2002-01-01 ', ' yyyy-mm-dd ')
);

--Get the number of days in a year

Select To_char (to_date (' 2000-01-01 ', ' yyyy-mm-dd ') + (rownum-1), ' yyyy-mm-dd ') date from user_objects where rownum<367 and To_date (' 2000-01-01 ', ' yyyy-mm-dd ') + (rownum-1) <to_date (' 2001-01-01 ', ' yyyy-mm-dd ');


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.