- 我在sql server中需要像F11.20,F13.20,F14.10这样的数据
declare @S varchar(200) = ',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom';
select left(@S, charindex(':', @S, charindex(':', @S)+2)-2);
答案 0 :(得分:4)
任何分割/解析函数都可以解决问题,但您必须执行辅助逻辑来清理已解析的字符串。也就是说,我修改了一个解析函数来接受任何两个非类似的分隔符(开始/结束)。在这种情况下,a和:
此外,作为一个表格值函数,很容易合并到一个交叉应用程序中或作为一个独立程序,如下所示。
示例强>
Select NewString = Stuff((Select ',' +RetVal
From [dbo].[udf-Str-Extract](@S,',',':')
For XML Path ('')),1,1,'')
<强>返回强>
F11.20,F13.20,F14.10
感兴趣的UDF
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 *,RetVal = Substring(@String, N, L) From cte4) 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,'[[',']]')
*/
编辑只是为了帮助实现可视化
如果您单独执行TVF:
declare @S varchar(200) = ',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom';
Select * From [dbo].[udf-Str-Extract](@S,',',':')
<强>返回强>
RetSeq RetPos RetVal
1 2 F11.20
2 10 F13.20
3 38 F14.10
编辑2 - 通过交叉申请执行
Declare @YourTable table (ID int,SomeString varchar(200))
Insert Into @YourTable values
(1,',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom'),
(2,',Z99.55:,Z25.10:Someother text')
Select A.ID
,B.*
From @YourTable A
Cross Apply (
Select NewString = Stuff((Select ',' +RetVal
From [dbo].[udf-Str-Extract](A.SomeString,',',':')
For XML Path ('')),1,1,'')
) B
<强>返回强>
ID NewString
1 F11.20,F13.20,F14.10
2 Z99.55,Z25.10
答案 1 :(得分:1)
如果您提取的文本总是以(字母,数字,数字,。,数字,数字)的形式出现,并且该文本总是有3个实例,那么您可以这样做:
WITH
s1(string, ci) AS (SELECT @S, CHARINDEX(':', @S)),
s2(ci) AS (SELECT CHARINDEX(':', @S, ci+1) FROM s1),
s3(ci) AS (SELECT CHARINDEX(':', @S, ci+1) FROM s2)
SELECT
SUBSTRING(string, s1.ci-6, 6)+','+
SUBSTRING(string, s2.ci-6, 6)+','+
SUBSTRING(string, s3.ci-6, 6)
FROM s1, s2, s3;
执行计划:
它没有那么高效。
如果它总是在任何“:”实例之前的6个字符,你可以抓取NGrams8K的副本并执行此操作:
declare @S varchar(200) = ',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom';
SELECT NewString = STUFF
((SELECT ','+SUBSTRING(@S, position-6, 6)
FROM dbo.NGrams8k(@S, 1)
WHERE token = ':'
FOR XML PATH('')),1,1,'');
使用NGrams8K和变量的另一种方式:
declare @S varchar(200) = ',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom';
declare @newstring varchar(100)='';
declare @S varchar(200) = ',F11.20:,F13.20:Sedative, hypnotic o,F14.10:Cocaine abuse, uncom';
declare @newstring varchar(100)='';
SELECT
@newstring +=
CASE @newstring WHEN '' THEN '' ELSE ',' END +SUBSTRING(@S, position-6, 6)
FROM dbo.NGrams8k(@S, 1)
WHERE token = ':';
SELECT @newstring;