将值与数据列分开

时间:2013-11-27 05:42:10

标签: database plsql

我有一个包含

等数据的专栏
I~A~G~S

我如何使用选择查询分隔它们。

输出: 的更新

  Rows       Column(Data)
  First      I
  Second     A
  Third      G
  Fourth     S

2 个答案:

答案 0 :(得分:2)

declare @s varchar(10)
set @s='I~A~G~S'

select replace(@s,'~',' ')

代表updated question

create table #vij11 (s varchar(100))

insert into #vij11(s) values ('I~A~G~S')
SELECT   
     Split.a.value('.', 'VARCHAR(100)') AS String  
 FROM  (SELECT [s],  
         CAST ('<M>' + REPLACE([s], '~', '</M><M>') + '</M>' AS XML) AS String  
     FROM  #vij11) AS A CROSS APPLY String.nodes ('/M') AS Split(a); 

fiddle demo

答案 1 :(得分:2)

如果您使用的是oracle数据库,那么它应该如下:

select replace('I~A~G~S','~',' ') as x from dual;

select replace(<fieldname>,'~',' ') from <tablename>

关于在不同行中拆分字符串的问题,请检查以下查询。

SELECT trim(regexp_substr(replace('I~A~G~S','~',','), '[^,]+', 1, LEVEL)) str_2_tab
  FROM dual
  CONNECT BY LEVEL <= regexp_count(replace('I~A~G~S','~',','), ',')+1;