如何从SQL Server中的长字符串中获取ID

时间:2016-10-25 16:00:39

标签: sql sql-server

在下面的字符串中,我需要你的帮助才能知道如何使用T-SQL查询获取或仅选择GUIDNVARCHAR(36))?

N'yes#red#A5257199-8B09-44F1-8073-C5D5F02126F9#No#fuchsia#A5A5B2F2-1B87-4B0E-85B6-16A287814574#'

2 个答案:

答案 0 :(得分:0)

创建一个拆分字符串函数以将数据拆分为不同的行,然后检查该行是否为unquieidentfier然后使用它。

该功能参考了本文Tally OH! An Improved SQL 8K “CSV Splitter” Function

CREATE FUNCTION [dbo].[DelimitedSplit8K]
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 0 up to 10,000...
     -- enough to cover NVARCHAR(4000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO

提取GUID

DECLARE @str NVARCHAR(4000)= N'yes#red#A5257199-8B09-44F1-8073-C5D5F02126F9#No#fuchsia#A5A5B2F2-1B87-4B0E-85B6-16A287814574#'

SELECT item
FROM   dbo.[Delimitedsplit8k](@str, '#')
WHERE  Try_cast(item AS UNIQUEIDENTIFIER) IS NOT NULL 

如果您使用SQL SERVER 2016,则可以使用STRING_SPLIT功能代替dbo.[Delimitedsplit8k]

答案 1 :(得分:0)

如果您真的不想使用Prdp建议的功能(老实说这是正确的方法),您可以执行以下操作

Declare @YourTable table (ID int,SomeString varchar(max))
Insert into @YourTable values
(1,'yes#red#A5257199-8B09-44F1-8073-C5D5F02126F9#No#fuchsia#A5A5B2F2-1B87-4B0E-85B6-16A287814574#')

Select A.ID
      ,GUIDs = B.RetVal 
 From @YourTable A
 Cross Apply (
                Select RetSeq = Row_Number() over (Order By (Select null))
                      ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                From (Select x = Cast('<x>'+ Replace(A.SomeString,'#','</x><x>')+'</x>' as xml).query('.')) as A 
                Cross Apply x.nodes('x') AS B(i)

             ) B Where Len(RetVal)=36

返回

ID  GUIDs
1   A5257199-8B09-44F1-8073-C5D5F02126F9
1   A5A5B2F2-1B87-4B0E-85B6-16A287814574