[英]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
您可以使用to_char()
function将日期转换为您需要的任何格式,包括季度:
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"
是字符文字,不要与未带引号的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
原始日期 | 绝对日期 | 四分之一 | 月 |
---|---|---|---|
01-FEB-22 | 01-02-2022 | 2022 年第一季度 | 02/2022 |
21 年 3 月 30 日 | 30-03-2021 | 2021 年第一季度 | 03/2021 |
20 年 12 月 29 日 | 29-12-2020 | 2020年第四季度 | 12/2020 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.