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

Hi Red987,

Please also find another method which has the same query cost with Naomi's query from below.

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 Sales were happened: SPANISH<UV>END')

select case when CHARINDEX('Number="',htmlString)>0 then
SUBSTRING(htmlString,CHARINDEX('Number="',htmlString)+len('Number="'),CHARINDEX('">',htmlString)-CHARINDEX('Number="',htmlString)-len('Number="'))
else '' end [Order Numbers],
case when CHARINDEX('Number="',htmlString)>0 then
SUBSTRING(htmlString,CHARINDEX('">',htmlString)+len('">'),CHARINDEX('</a>',htmlString)-CHARINDEX('">',htmlString)-len('">'))
else '' end [Value]
from @t

/*
Order Numbers	Value
1012,1013,1014,1015	4
1011,1012,1013,1014,1015,1016,1017,1018,1019,1020	10
	0
*/

Wishes

Melissa


""SQL Server related"" forum will be migrated to a new home onMicrosoft Q&A SQL Server!
We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server!
For more information, please refer to the sticky post.




Viewing all articles
Browse latest Browse all 34

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>