我有一個多行 nvarchar,其中包含以下格式的字串:
@String =
'ReportHeaderColor{R:98, B:183, G:178}
ReportHeaderFontColor{R:255, B:255, G:255}
ReportBodyHeaderColor{R:98, B:183, G:178}
ReportBodyFontColor{R:255, B:255, G:255}'
我希望能夠使用 nvarchar 值 @Attribute = 'ReportHeaderFontColor' 并僅從包含 @Attribute 值的 @String 中提取行
uj5u.com熱心網友回復:
只是另一種方法
例子
Declare @String varchar(max)=
'ReportHeaderColor{R:98, B:183, G:178}
ReportHeaderFontColor{R:255, B:255, G:255}
ReportBodyHeaderColor{R:98, B:183, G:178}
ReportBodyFontColor{R:255, B:255, G:255}'
Declare @Find varchar(150) = 'ReportHeaderFontColor'
Select value
From string_split(replace(replace(@String,'}','}|'),char(13) char(10),''),'|')
where charindex(@Find,value)>0
結果
value
ReportHeaderFontColor{R:255, B:255, G:255}
uj5u.com熱心網友回復:
如何使用以下方法apply來查找要查找和substring提取的開始/結束位置:
declare @Attribute nvarchar(100)= 'ReportHeaderFontColor',
@String nvarchar(max) =
'ReportHeaderColor{R:98, B:183, G:178}
ReportHeaderFontColor{R:255, B:255, G:255}
ReportBodyHeaderColor{R:98, B:183, G:178}
ReportBodyFontColor{R:255, B:255, G:255}';
with s as (select @string as string)
select Substring(string,b.pos,e.pos-b.pos 1)
from s
cross apply(values(charindex(Concat('ReportBodyHeaderColor','{'), string)))b(pos)
cross apply(values(CharIndex('}', string, b.pos)))e(pos)
where b.pos>0;
結果:
ReportHeaderFontColor{R:255, B:255, G:255}
uj5u.com熱心網友回復:
您可以按換行符拆分字串char(10),然后char(13)按如下方式洗掉回車符:
Declare @Attribute nVarChar(50)= 'ReportHeaderFontColor'
Declare @String nVarChar(max) =
'ReportHeaderColor{R:98, B:183, G:178}
ReportHeaderFontColor{R:255, B:255, G:255}
ReportBodyHeaderColor{R:98, B:183, G:178}
ReportBodyFontColor{R:255, B:255, G:255}'
Select Replace(Value,Char(13),'') As Result
From String_Split(@String,Char(10))
Where Value Like Concat('%',@Attribute,'%')
| 結果 |
|---|
| ReportHeaderFontColor{R:255, B:255, G:255} |
uj5u.com熱心網友回復:
感謝您的所有建議,不幸的是我無法使用 string_split,因為我們的一些客戶端使用的系統早于 SQL 2016。
在玩了一會兒之后,我能夠想出以下解決方案,我將其放置在 UDF 中并添加了一些錯誤檢查:
declare @header_row nvarchar(max) =
'ReportHeaderColor{R:98, B:183, G:178}
ReportHeaderFontColor{R:255, B:255, G:255}
ReportBodyHeaderColor{R:98, B:183, G:178}
ReportBodyFontColor{R:255, B:255, G:255}'
declare @Attribute nvarchar(max) = 'ReportHeaderFontColor'
declare @String nvarchar(255)
-- check that the header row is not empty and actually contains the attribute
if (len(@header_row) = 0 or @header_row not like '%' @Attribute '%')
begin
select @String = null
end
else begin
select @String =
replace(replace(replace(
left(
substring(@header_row,charindex(@Attribute '{', @header_row) len(@Attribute '{'), 999),charindex('}',
substring(@header_row,charindex(@Attribute '{', @header_row) len(@Attribute '{'), 999))-1)
,'R:',''),'G:',''),'B:','')
-- if the string is not in the correct format(3 integers separated by a comma) set @String to null
if (select count(1)
from dbo.fn_ListToTable(@String,',') x
where ISNUMERIC(x.List) = 1 and x.List <= 255) <> 3
begin
set @String = null
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402680.html
標籤:
上一篇:每年的MAXAVG分數
