简体   繁体   中英

How to get Year/Week according to ISO?

I have the below code, which gives week 202153. However, I would need to have, instead of the first week of year 2021 as 202153, as 202053.

Is there any built-in functionality to address this?

SELECT
    to_char(TO_DATE('20210104', 'YYYYMMDD') - 3, 'YYYYIW') AS yearweek
FROM
    dual

Expected output: 202053. I could simply decode the value, but I'm hoping for a more robust solution for future years.

Use IYYY (ISO-year) rather than the YYYY (calendar year) format model.

SELECT TO_CHAR( DATE '2021-01-04' - 3, 'IYYYIW') AS yearweek
FROM   DUAL

Which outputs:

YEARWEEK
202053

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM