2016-04-11 53 views
0

作爲Oracle數據庫中的新手,我懇求您的幫助。oracle數據庫:按字符分割字符串

我需要一個字符串分割成不同的記錄爲每個卡拉科特:

我有一個VARCHAR領域,我需要分割成幾個字符

select 
    MAPS.MAP_ID, 
    HARD_BIN_LINES.LINE, HARD_BIN_LINES.BINS 
from MAPS, HARD_BIN_LINES 
where MAPS.MAP_ID = 9595435 and MAPS.MAP_ID = HARD_BIN_LINES.MAP_ID 
order by HARD_BIN_LINES.MAP_ID, HARD_BIN_LINES.LINE 


MAP_ID LINE BINS 
9595435 1  ÿÿÿÿÿÿÿÿÿÿÿþþþþÿÿÿÿÿÿÿÿÿÿÿ 
9595435 2  ÿÿÿÿÿÿÿþþ  þþÿÿÿÿÿÿÿÿ 
9595435 3  ÿÿÿÿÿþþ2   þÿÿÿÿÿÿ 
9595435 4  ÿÿÿÿþ    þþÿÿÿÿ 
9595435 5  ÿÿÿþ2     þÿÿÿ 
9595435 6  ÿÿþ     þÿÿ 
9595435 7  ÿÿþ2     þÿÿ 
9595435 8  ÿþþÿ     þþÿ 
9595435 9  ÿ2      þÿ 
9595435 10  þÿ      þÿ 
9595435 11  þ      þ 
9595435 12  ü      þ 
9595435 13  ü2      þ 
9595435 14  þ      þ 
9595435 15  þ      ÿ 
9595435 16  ÿþ    xx  þÿ 
9595435 17  ÿþ      þÿ 
9595435 18  ÿÿþ     þÿÿ 
9595435 19  ÿÿþ     þÿÿ 
9595435 20  ÿÿÿþ     þÿÿÿ 
9595435 21  ÿÿÿÿþ    þþÿÿÿÿ 
9595435 22  ÿÿÿÿÿþ  þ 2 þÿÿÿÿÿÿ 
9595435 23  ÿÿÿÿÿÿÿþ þ þþ ÿÿÿÿÿÿÿÿ 
9595435 24  ÿÿÿÿÿÿÿÿÿÿÿþþþþÿÿÿÿÿÿÿÿÿÿÿ 

我的目標是包含查詢分分檔記錄成幾個字母obtaing這樣

MAP_ID LINE LEVEL CHR BINCODE 
-------+-------+-------+---+-------- 
9595435 1  2  ÿ 255 
9595435 1  3  ÿ 255 
9595435 1  4  ÿ 255 
9595435 1  5  ÿ 255 
9595435 1  6  ÿ 255 
9595435 1  7  ÿ 255 
9595435 1  8  ÿ 255 
9595435 1  9  ÿ 255 
9595435 1  10  ÿ 255 
9595435 1  11  ÿ 255 
9595435 1  12  þ 254 
9595435 1  13  þ 254 
9595435 1  14  þ 254 
9595435 1  15  þ 254 
9595435 1  16  ÿ 255 
9595435 1  17  ÿ 255 
9595435 1  18  ÿ 255 
9595435 1  19  ÿ 255 
9595435 1  20  ÿ 255 
9595435 1  21  ÿ 255 
9595435 1  22  ÿ 255 
9595435 1  23  ÿ 255 
9595435 1  24  ÿ 255 
9595435 1  25  ÿ 255 
9595435 1  26  ÿ 255 
-------+-------+-------+---+-------- 
9595435 2  2  ÿ 255 
9595435 2  3  ÿ 255 
9595435 2  4  ÿ 255 
9595435 2  5  ÿ 255 
9595435 2  6  ÿ 255 
9595435 2  7  ÿ 255 
9595435 2  8  þ 254 
9595435 2  9  þ 254 
9595435 2  10   1 
9595435 2  11   1 
9595435 2  12   10 
9595435 2  13   1 
9595435 2  14   13 
9595435 2  15   17 
9595435 2  16   1 
9595435 2  17  þ 254 
9595435 2  18  þ 254 
9595435 2  19  ÿ 255 
9595435 2  20  ÿ 255 
9595435 2  21  ÿ 255 
9595435 2  22  ÿ 255 
9595435 2  23  ÿ 255 
9595435 2  24  ÿ 255 
9595435 2  25  ÿ 255 
9595435 2  26  ÿ 25 
-------+-------+-------+---+-------- 
(...) 

一個記錄如果我試圖解決與打發出「CONNECT BY」語句我獲得很多duplica TE記錄,我不明白爲什麼沒有什麼是錯我的查詢:

select level, substr('Stefano', level, 1) /* a substring starting from level-th character, 1 character log */ 
from dual 
connect by level <= length('Stefano') /* the same number of rows than the length of the string */ 

這將構建一個排爲開始的字符串,其中的每個字符:

with temp as (
    select 
     MAPS.MAP_ID, 
     HARD_BIN_LINES.LINE, HARD_BIN_LINES.BINS 
    from MAPS, HARD_BIN_LINES 
    where MAPS.MAP_ID = 9595435 and MAPS.MAP_ID = HARD_BIN_LINES.MAP_ID 
    order by HARD_BIN_LINES.MAP_ID, HARD_BIN_LINES.LINE 
) 
select 
    MAP_ID, LINE, LEVEL, 
    substr(BINS,level,1) as CHR, ASCII(substr(BINS,level,1)) as BINCODE 
from temp 
connect by level <= length(BINS) 

回答

2

你可以用這樣的嘗試第N行包含第N個字符,由substr提取。