Oracle 11:SQL:246个字节(嘿,至少我击败了Java:P lol)
with d as(select to_date('&1','yyyy-mm-dd')s,to_date('&2','yyyy-mm-dd')e from dual),r as(select level-1 l from d connect by level<=e-s+1),x as(select s+l y,to_char(s+l,'yyyymmdd')w from d,r)select to_char(y,'yyyy-mm-dd')from x where w=reverse(w);
输出:
SQL> with d as(select to_date('&1','yyyy-mm-dd')s,to_date('&2','yyyy-mm-dd')e from dual),r as(select level-1 l from d connect by level<=e-s+1),x as(select s+l y,to_char(s+l,'yyyymmdd')w from d,r)select to_char(y,'yyyy-mm-dd')from x where w=reverse(w);
Enter value for 1: 2000-01-01
Enter value for 2: 2021-01-01
TO_CHAR(Y,
----------
2001-10-02
2010-01-02
2011-11-02
2020-02-02
SQL>
可读格式:
with d as (select to_date('&1','yyyy-mm-dd') s,
to_date('&2','yyyy-mm-dd') e
from dual),
r as (select level-1 l
from d connect by level <= e-s+1),
x as (select s+l y, to_char(s+l,'yyyymmdd') w
from d,r)
select to_char(y,'yyyy-mm-dd')
from x
where w=reverse(w);
解释:
d: get input for start/end
r: generate rows needed, 1 per day.
x: calculate the actual dates, and convert them to a minimal string.
final: use REVERSE function to verify the palindroms, return in proper format.
今天了解了REVERSE功能:)
DATE
避免使用ato_date()
还是必须使用较少使用golfable的类型VARCHAR2
?