我有一个SQL Server列,其中包含
之类的值 [157;#10 - S1 Pawl Cap],
[168;#11 - S2 Two],
[180;#11 - S2 TwoB],
[153;#11-H3 Snowboard],
[173;#12 - Fishing (CA Surf Casting)],
[155;#12 - Fishing (Colorado)],
[162;#12 - H3 Snow],
[164;#12 - Internal Mono/Silver L5],
[158;#12 - L5 Bike],
[163;#12 - L5 Casual],
[156;#12 - L5 Kids],
[174;#12 - Nordic],
[159;#12 - S2 Snap In Bike],
[160;#12 - S2 Snap In Casual],
[161;#12 - Treksta EX735],
[185;#13 - 2nd Taste of Boa - External Test],
[170;#13 - Fishing],
[176;#13 - Gaerne Moto],
[188;#13 - IP1 - FT 1 Bike and Run],
[177;#13 - IP1 - FT 2 Bike],
[178;#13 - IP1 - FT 2 Running],
[154;#13 - Mono Stage 1 - Filament 2 Running],
[175;#13 - Mono Stage 2 - Filament 3 Casual],
[169;#13 - Mono Stage 2 - Filament 3 Running],
[184;#13 - Retrofit Day - 4/5/13 Internal Test],
[165;#13 - Vasque Open Race],
[191;#13 - Winter HP88],
[181;#13 - Winter NER],
[192;#13 - Winter Webbing],
[190;#13 - Zonal 1],
[205;#14 - Cassowary],
[207;#14 - DC Motocross Project],
[204;#14 - Fishing (HP88SW)],
[197;#14 - Golden Eagle],
[198;#14 - Golf Pants Catching],
[179;#14 - GOLF Test],
[200;#14 - IP1 Agro Road Bike],
[202;#14 - IP1 FT3],
[182;#14 - L6 FT1],
[193;#14 - L6 FT2],
[183;#14 - Landscaping (Utility)],
[195;#14 - No Sew Guides],
[210;#14 - PC Replacement],
[167;#14 - Running Suite FT ZERO],
[166;#14 - Specialized Suite],
[201;#14 - Track Spike],
[203;#14 - Winter Snow],
[216;#15 - American Football],
[213;#15 - Boa Kids],
[196;#15 - Cassowary],
[212;#15 - Golf (IP1 Heel)],
[215;#15 - L5 Running No Sew],
[211;#15 - L6 No Sew],
[208;#15 - L6 T1 Shots],
[209;#15 - Mountaineering EMEA],
[186;#15 - Redwing Utility Test],
[187;#15 - Run Suite FT2],
[206;#15 - Running NC722 Color],
[217;#15 - S2-S No-Sew],
[219;#15 - Snowboard],
[221;#16 - Fit Lab],
[225;#16 - Golf TX],
[222;#16 - Internal FormTX],
[228;#16 - OBG Snow],
[227;#16 - OG Running],
[194;#16 - OG Trail],
[224;#16 - PVDF Lace],
[214;#16 - Run Suite FT2 Two],
[171;#16 - S3 Bike],
[223;#16 - Summer Snow],
[218;#16 - Trudel Fit Test],
[226;#16 - TX5 Utility],
[220;#16 - Unbranded Running],
[189;#16 - Winter Snow],
[199;#17 - M4],
[172;#17 - S3 FT2]
我想选择[
和;
之间的所有重复值。
从上面我想选择157,168等等。
答案 0 :(得分:1)
我修改了这些项目的分割/解析功能。我们传递两个(开头和结尾)而不是传递一个分隔符。这些分隔符可以是任何非类似的字符串。例如<AssemblyDate>
和<div>
或您的情况 [和; 。
示例强>
</div>
<强>返回强>
感兴趣的UDF
-- Original String Truncated for Demo
Declare @String varchar(max) = '[157;#10 - S1 Pawl Cap],[168;#11 - S2 Two],[180;#11 - S2 TwoB],[153;#11-H3 Snowboard]'
Select * from [dbo].[udf-Str-Extract](@String,'[',';')
编辑 - 如果需要逗号分隔字符串
CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table
As
Return (
with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By N)
,RetPos = N
,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
From (Select A.N,RetVal = ltrim(rtrim(Substring(@String, A.N, A.L))) From cte4 A ) A
Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters
Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/
返回
Declare @YourTable table (ID int,YourCol varchar(max))
Insert Into @YourTable values
(1,'[157;#10 - S1 Pawl Cap],[168;#11 - S2 Two],[180;#11 - S2 TwoB],[153;#11-H3 Snowboard]')
Select A.ID
,B.String
From @YourTable A
Cross Apply (
Select String = Stuff((Select Distinct ',' +RetVal
From [dbo].[udf-Str-Extract](YourCol,'[',';')
For XML Path ('')),1,1,'')
) B