SQL 查询以从日期获取季度和 month/year
SQL query to get quarter and month/year from date
在一列中我有日期 - 01-02-2022, 01-03-2022
我怎样才能得到下面格式的这个
ABS_DATE Quarter Month
01-02-2022 Q1 2022 02/2022
30-03-2021 Q1 2021 03/2021
29-12-2020 Q4 2020 12/2020
您可以使用the to_char()
function to convert your dates to whatever format您需要的,包括宿舍:
to_char(abs_date, 'DD-MM-YYYY') as abs_date
to_char(abs_date, '"Q"Q YYYY') as quarter
to_char(abs_date, 'MM/YYYY') as month
带引号的 "Q"
是 a character literal,不要与未带引号的 Q
混淆,后者是“Quarter of year”的元素。
使用 CTE 获取示例数据的演示:
with your_table (abs_date) as (
select date '2022-02-01' from dual
union all
select date '2021-03-30' from dual
union all
select date '2020-12-29' from dual
)
select abs_date as raw_date,
to_char(abs_date, 'DD-MM-YYYY') as abs_date,
to_char(abs_date, '"Q"Q YYYY') as quarter,
to_char(abs_date, 'MM/YYYY') as month
from your_table
RAW_DATE
ABS_DATE
QUARTER
MONTH
01-FEB-22
01-02-2022
Q1 2022
02/2022
30-MAR-21
30-03-2021
Q1 2021
03/2021
29-DEC-20
29-12-2020
Q4 2020
12/2020
在一列中我有日期 - 01-02-2022, 01-03-2022
我怎样才能得到下面格式的这个
ABS_DATE Quarter Month
01-02-2022 Q1 2022 02/2022
30-03-2021 Q1 2021 03/2021
29-12-2020 Q4 2020 12/2020
您可以使用the to_char()
function to convert your dates to whatever format您需要的,包括宿舍:
to_char(abs_date, 'DD-MM-YYYY') as abs_date
to_char(abs_date, '"Q"Q YYYY') as quarter
to_char(abs_date, 'MM/YYYY') as month
带引号的 "Q"
是 a character literal,不要与未带引号的 Q
混淆,后者是“Quarter of year”的元素。
使用 CTE 获取示例数据的演示:
with your_table (abs_date) as (
select date '2022-02-01' from dual
union all
select date '2021-03-30' from dual
union all
select date '2020-12-29' from dual
)
select abs_date as raw_date,
to_char(abs_date, 'DD-MM-YYYY') as abs_date,
to_char(abs_date, '"Q"Q YYYY') as quarter,
to_char(abs_date, 'MM/YYYY') as month
from your_table
RAW_DATE | ABS_DATE | QUARTER | MONTH |
---|---|---|---|
01-FEB-22 | 01-02-2022 | Q1 2022 | 02/2022 |
30-MAR-21 | 30-03-2021 | Q1 2021 | 03/2021 |
29-DEC-20 | 29-12-2020 | Q4 2020 | 12/2020 |