如何在Oracle中将垂直字符串转换为水平

时间:2019-04-11 05:00:15

标签: sql database oracle

O N K A R

如何将其转换为ONKAR。相反,我知道。但这我无法解决。

4 个答案:

答案 0 :(得分:3)

无法通常无法做您想做的事,而又没有第二列来提供每个字母的顺序。假设您确实有该职位的专栏,我们可以尝试:

SELECT LISTAGG(letter, '') WITHIN GROUP (ORDER BY position) word
FROM yourTable;

Demo

数据:

letter | position
O      | 1
N      | 2
K      | 3
A      | 4
R      | 5

答案 1 :(得分:0)

对于长度不超过4000字节的字符串,Listagg是正确的解决方案,因为它返回varchar2数据类型。但是对于更长的字符串,您可能会获得clob数据类型。

with s (letter, position) as (
select 'O', 1 from dual union all
select 'N', 2 from dual union all
select 'K', 3 from dual union all
select 'A', 4 from dual union all
select 'R', 5 from dual)
select xmlcast(xmlagg(xmlelement(x, letter) order by position) as clob) c
from s;

C              
---------------
ONKAR

答案 2 :(得分:0)

只要数据结果具有要粘贴在一起的所有行,就可以使用它。

with data as (select 'O' as  letter from dual
              union all
              select 'N' from dual
              union all
              select 'K' from dual
              union all
              select 'A' from dual
              union all
              select 'R' from dual)
SELECT LISTAGG(letter, '') WITHIN GROUP (ORDER BY rownum)
FROM data;

答案 3 :(得分:0)

如果您的数据位于用换行符(ASCII 13)字符分隔的一行中,则只需使用REPLACE( value, CHR(13) )

Oracle设置

CREATE TABLE test_data ( value ) AS
SELECT 'O' || CHR(13) || 'N' || CHR(13) || 'K' || CHR(13) || 'A' || CHR(13) || 'R' FROM DUAL

查询

SELECT value, REPLACE( value, CHR(13) ) FROM test_data

输出

VALUE     | REPLACE(VALUE,CHR(13))
:-------- | :---------------------
O         | ONKAR                 
N         |
K         |
A         |
R         |

db <>提琴here