使用分隔符在SQL Server中拆分字符串

时间:2014-04-25 07:16:37

标签: sql sql-server split

我的字符串如下:

<SL1> Locationdetails are in the table .<RXCH><cF7712>Location1<cF4712>
        <RXCS1>H1<RXCS2>H2<RXCS3>H3
        <RXC11>C11<RXC12>C12
        <RXC13>C13<RXC21>C21
        <RXC22>C22<RXC23>C23
        <RXC31>C31<RXC32>C32
        <RXC33>C33<RXC41>C41
        <RXC42>C42<RXC43>C43
        <RXC51>C51<RXC52>C52
        <RXC53>C53
        <RXCH><cF7712>Location1<cF4712>
        <RXCS1>H1<RXCS2>H2<RXCS3>H3
        <RXC11>C11<RXC12>C12
        <RXC13>C13<RXC21>C21
        <RXC22>C22<RXC23>C23
        <RXC31>C31<RXC32>C32
        <RXC33>C33<RXC41>C41
        <RXC42>C42<RXC43>C43
        <RXC51>C51<RXC52>C52
        <RXC53>C53. Services for primary health care'

我想将上面的字符串和输出拆分如下:

OUTPUT应为:

'Location1 H1 H2 H3 C11 C12 C13 C21 C22 C23 C31 C32 C33 C41 C42 C43 C51 C52 C53 Location1 H1 H2 H3 C11 C12 C13 C21 C22 C23 C31 C32 C33 C41 C42 C43 C51 C52 C53'

1 个答案:

答案 0 :(得分:1)

-- Get text between points
set @str = stuff (@str, 1, charindex ('.', @str), '')
set @str = left (@str, charindex ('.', @str) - 1)
-- Get rid of spaces, tabs and line ends
set @str = replace (replace (replace (replace (@str, ' ', ''), char(9), ''), char(10), ''), char(13), '')
-- Output
select ltrim (cast(replace (@str, '>', '/> ') as xml).value('.', 'varchar(max)'))