我有一个表,它有一个列years,数据类似于2016/17;2017/18等等。
到目前为止,我有这样的代码:
SELECT champion
FROM championship
WHERE season = (select to_char(sysdate - 730, 'yyyy') from dual)例如,我想选择两年前的冠军。我怎么能这么做?
我本想分拆2016/17年度(这是个怪人),但怎么才能拿到2016年的票呢?
这是一个“简单”的问题,所以,我想不做任何功能或类似的事情。
发布于 2018-12-03 10:34:50
您只需使用substr:
SELECT champion
FROM championship
WHERE substr(season,1,4) = (to_char(sysdate - interval '2' YEAR,'YYYY') from dual)to_char(sysdate - interval '2' YEAR,'YYYY')优于to_char(sysdate - 730, 'yyyy'),例如:
+------------+------------+---------+
+ Date | -730 day + -2 year +
+------------+------------+---------+
| 2018-12-31 | 2016-12-31 + 2016 +
| 2019-12-31 | 2017-12-31 + 2017 +
| 2020-12-31 | 2019-01-01 + 2018 + ==> different year
| 2021-12-31 | 2020-01-01 + 2019 + ==> different year
| 2022-12-31 | 2020-12-31 + 2020 +
| 2023-12-31 | 2021-12-31 + 2021 +
| 2024-12-31 | 2023-01-01 + 2022 + ==> different year
| 2025-12-31 | 2024-01-01 + 2023 + ==> different year
+------------+------------+---------+发布于 2018-12-03 11:01:38
不要使用sysdate - 730,而是使用INTERVAL年份。此外,如果季节总是以SUBSTR形式出现,则不需要<year/year+1>提取前4个字符。
WHERE season =
TO_CHAR(SYSDATE - INTERVAL '2' YEAR,'YYYY') ||'/'||
TO_CHAR(SYSDATE - INTERVAL '1' YEAR,'YY')https://stackoverflow.com/questions/53591889
复制相似问题