select 'V4A-V4G' FROM DUAL;
sample output:
V4A
V4B
V4C
V4D
V4E
V4G
select 'R8M-R8S' FROM DUAL;
sample output:
R8M
R8N
R8O
R8P
R8Q
R8R
R8S
uj5u.com熱心網友回復:
這里有一個選擇:對于你發布的樣本資料(我把它們放到同一個test CTE中),找到第3個和最后一個字母的ASCII碼(正如你所說--在一個評論中--它們只被使用),并做一點行生成器的計算。
SQL> with test(id, col) as
2 (select 1, 'V4A-V4G' from dual union all
3 select 2, 'R8M-R8S' from double
4 )
5 select id,
6 substr(col, 1, 2) || chr(ascii(substr(col, 3, 1) column_value - 1) val
7 from test cross join
8 table(cast(multiset(select level from double
9 connect by level <= ascii(substr(col, -1)) - ascii(substr(col, 3, 1) 1
10 ) as sys.odcinumberlist))
11 order by id, val;
ID VAL
---------- --
1 V4A
1 V4B
1 V4C
1 V4D
1 V4E
1 V4F
1 V4G
2 R8M
2 R8N
2 R8O
2 R8P
2 R8Q
2 R8R
2 R8S
14 rows 選擇。
SQL>
uj5u.com熱心網友回復:
一個更直接的方法(避免了CONNECT BY程序):
with
test (id, col) as (
select 1, 'V4A-V4G' from dual union all
select 2, 'R8M-R8S' from dual
)
select id, substr(col, 1, 2) || column_valueas val
from test join sys. odcivarchar2list('A'/span>,'B'/span>,'C'/span>,'D'/span>, 'E','F','G','H','I',
'J','K','L','M'。 'N'/span>,'O'/span>,'P'/span>,'Q'/span>。 'R'/span>,'S'/span>,'T'/span>,'U'/span>。 'V','W','X','Y','Z')
on column_value between substr(col, 3, 1) and substr(col, 7, 1)
order by id, val --如果需要的話。
;
當然,如果你需要經常這樣做,你可以使用一個實際的表,其中有一列和26行存放大寫字母,這樣你就不需要在每個使用它的查詢中臨時創建它(以及每次使用查詢時)。這將使查詢變得更加簡單。
注意 - 在舊的 Oracle 版本中,你可能需要將 sys.odci...list 包在 table( ... ) 中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/308118.html
標籤:
下一篇:將查詢結果存盤在一個表中
