如何在没有函数的情况下将字符串拆分为SQL中的行

时间:2018-06-08 13:29:07

标签: sql sql-server

我有以下字符串

  

技术 | faa5d304-f2d1-42c3-8d21-e87697b42bdc;的应用 | 56b19e9a-e58a-4c79-A518-b129fb5f499f;的数据库 | d7425391-8f8c-4aec-be04-9caf2f55584a; 移动/ BYOD | 8f0f30e7-d16d-48a3-ad82-cfdd39156760;的网络 | 3876dbd8-8cd8-4040-9c67-0633f8477f93; 操作系统 | 10fc2ce4-53fd-4af2-8fd9-9df66a38715f; 报告 | 00307182-43f4-4bbf-9a95-cd8dbf59754a; 安全性 | 014e8d4d- 4fd9-404c-8db8-13e84c9042fe; 用户界面 | 57d65a47-6ad2-4df7-8d36-acdf3e0a3145; Web Tech | 1b9e82eb-5f70-4183-9093-5

每个粗体字必须是一行。我正在使用不同的建议,但我只能检索第一个单词,技术。我需要连续每个大胆的单词。我需要在没有功能的情况下接近它。我正在使用SQL Server 2012.

2 个答案:

答案 0 :(得分:1)

使用答案linked to by scsimon,您可以使用以下脚本提取问题中的粗体字:

-- Test table
declare @t table (Id int identity(1,1), Col varchar(1000))
insert into @t(Col) values ('Technology|faa5d304-f2d1-42c3-8d21-e87697b42bdc;Application|56b19e9a-e58a-4c79-a518-b129fb5f499f;Database|d7425391-8f8c-4aec-be04-9caf2f55584a;Mobile/BYOD|8f0f30e7-d16d-48a3-ad82-cfdd39156760;Networking|3876dbd8-8cd8-4040-9c67-0633f8477f93;Operating System|10fc2ce4-53fd-4af2-8fd9-9df66a38715f;Reporting|00307182-43f4-4bbf-9a95-cd8dbf59754a;Security|014e8d4d-4fd9-404c-8db8-13e84c9042fe;User Interface|57d65a47-6ad2-4df7-8d36-acdf3e0a3145;Web Tech|1b9e82eb-5f70-4183-9093-5')
                          ,('asd|a;dse|a;gggg|a')

select t.Id
        ,n.r.value('.', 'varchar(50)') as String
        ,left(n.r.value('.', 'varchar(50)'),charindex('|',n.r.value('.', 'varchar(50)'),1)-1) as Words
        ,substring(n.r.value('.', 'varchar(50)'),charindex('|',n.r.value('.', 'varchar(50)'),1)+1,999999) as GUIDs
from @t as t
  cross apply (select cast('<r>'+replace(replace(Col,'&','&amp;'), ';', '</r><r>')+'</r>' as xml)) as S(XMLCol)
  cross apply S.XMLCol.nodes('r') as n(r)
order by t.Id
        ,Words;

输出:

+----+----------------------------------------------------+------------------+--------------------------------------+
| Id |                       String                       |      Words       |                GUIDs                 |
+----+----------------------------------------------------+------------------+--------------------------------------+
|  1 | Application|56b19e9a-e58a-4c79-a518-b129fb5f499f   | Application      | 56b19e9a-e58a-4c79-a518-b129fb5f499f |
|  1 | Database|d7425391-8f8c-4aec-be04-9caf2f55584a      | Database         | d7425391-8f8c-4aec-be04-9caf2f55584a |
|  1 | Mobile/BYOD|8f0f30e7-d16d-48a3-ad82-cfdd39156760   | Mobile/BYOD      | 8f0f30e7-d16d-48a3-ad82-cfdd39156760 |
|  1 | Networking|3876dbd8-8cd8-4040-9c67-0633f8477f93    | Networking       | 3876dbd8-8cd8-4040-9c67-0633f8477f93 |
|  1 | Operating System|10fc2ce4-53fd-4af2-8fd9-9df66a387 | Operating System | 10fc2ce4-53fd-4af2-8fd9-9df66a387    |
|  1 | Reporting|00307182-43f4-4bbf-9a95-cd8dbf59754a     | Reporting        | 00307182-43f4-4bbf-9a95-cd8dbf59754a |
|  1 | Security|014e8d4d-4fd9-404c-8db8-13e84c9042fe      | Security         | 014e8d4d-4fd9-404c-8db8-13e84c9042fe |
|  1 | Technology|faa5d304-f2d1-42c3-8d21-e87697b42bdc    | Technology       | faa5d304-f2d1-42c3-8d21-e87697b42bdc |
|  1 | User Interface|57d65a47-6ad2-4df7-8d36-acdf3e0a314 | User Interface   | 57d65a47-6ad2-4df7-8d36-acdf3e0a314  |
|  1 | Web Tech|1b9e82eb-5f70-4183-9093-5                 | Web Tech         | 1b9e82eb-5f70-4183-9093-5            |
|  2 | asd|a                                              | asd              | a                                    |
|  2 | dse|a                                              | dse              | a                                    |
|  2 | gggg|a                                             | gggg             | a                                    |
+----+----------------------------------------------------+------------------+--------------------------------------+

答案 1 :(得分:1)

试试这个:

DECLARE @Tabaldata TABLE ( data nvarchar(max))
INSERT INTO @Tabaldata
SELECT 
'Technology|faa5d304-f2d1-42c3-8d21-e87697b42bdc;Application|56b19e9a-e58a-4c79-a518-b129fb5f499f;Database
|d7425391-8f8c-4aec-be04-9caf2f55584a;Mobile/BYOD|8f0f30e7-d16d-48a3-ad82-cfdd39156760;Networking
|3876dbd8-8cd8-4040-9c67-0633f8477f93;Operating System|10fc2ce4-53fd-4af2-8fd9-9df66a38715f;Reporting|
00307182-43f4-4bbf-9a95-cd8dbf59754a;Security|014e8d4d-4fd9-404c-8db8-13e84c9042fe;User Interface|57d65a47-6ad2-4df7-8d36-acdf3e0a3145;Web Tech|1b9e82eb-5f70-4183-9093-5'


SELECT data ActualData, 
        SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data)) AS ExpectedData
FROM
(
    SELECT Split.a.value('.','nvarchar(max)') data
    FROM(
    SELECT CAST('<S>'+REPLACE(data,'|','</S><S>')+'</S>'  AS XML) data
    FROM @Tabaldata
    )AS A 
    CROSS APPLY data.nodes('S') AS Split(a)
)dt
WHERE PATINDEX('%[0-9]%',(SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data))))=0

演示结果:http://rextester.com/UXDT75928