Quantcast
Channel: Extract values from the String filed in SQL server
Viewing all articles
Browse latest Browse all 34

Extract values from the String filed in SQL server

$
0
0

I've adjusted the code, I'm getting null if the pattern doesn't match:

declare @t table (ID int identity(1,1) primary key, htmlString nvarchar(max))
insert into @t (htmlString) 
values ('Number of Sales: <Area="ABC" Number="1012,1013,1014,1015">4</a><ZZ>END'),
('Number of Sales: <Area="CBA" Number="1011,1012,1013,1014,1015,1016,1017,1018,1019,1020">10</a><YY>END'),
('Number of Sales: <Area="XBA" Number="">0</a><XX>END'),
('No pattern at all')

;with cte as (
select *, 
substring(htmlString, NULLIF(patindex('% Number="%">%', htmlString),0) +  len(' Number="'), len(htmlString)) as OrderNumbers,
substring(htmlString, NULLIF(patindex('%">%', htmlString),0) +  2, len(htmlString)) as NumberStart

from @t)

select *, 
substring(OrderNumbers, 1, NULLIF(PATINDEX('%">%', OrderNumbers),0)-1) as OrderNumbers,

substring(NumberStart, 1, NULLIF(PATINDEX('%</a>%', NumberStart),0)-1) as Number

from cte


Looking for new opportunities

For every expert, there is an equal and opposite expert. - Becker's Law


My blog

My TechNet articles

Viewing all articles
Browse latest Browse all 34

Latest Images

Trending Articles





Latest Images