Click here to Skip to main content
15,886,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a column in a table Inforamtion having value as 'text <head>record 1, record 2 </head> . HTML tag'.

What I want is that the string portion between <head> </head> tag.
means result should be record 1, record 2.


Thanks & Regards
Mohammad Wasif
Posted
Comments
dimpledevani 26-Mar-14 9:40am    
You can try the substring function.If all records have <head> and </head> and you required data is always between these tags,then substring would be apt.

Create Proc  StripHTML (@HTMLText VARCHAR(MAX))
 as
BEGIN
    DECLARE @Start INT
    DECLARE @End INT
    DECLARE @Length INT
    SET @Start = CHARINDEX('<',@HTMLText)
 
    SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
 
    SET @Length = (@End - @Start) + 1
    
    WHILE @Start > 0 AND @End > 0 AND @Length > 0
    BEGIN
        SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
       
        SET @Start = CHARINDEX('<',@HTMLText)
         
        SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
         
        SET @Length = (@End - @Start) + 1
    END
    Select LTRIM(RTRIM(@HTMLText))
END



 -- StripHTML '<head><body><form>record 1, record 2 </form></body></head>'
 
Share this answer
 
Comments
Mohd Wasif 27-Mar-14 1:05am    
Worked for me
Mohd Wasif 27-Mar-14 1:05am    
my +5
You can do it - but it's probably better done in your presentation software, or by storing the info in the format you want, because SQL string handling facilities are...um...basic.

SQL
SELECT SUBSTRING(
                 SUBSTRING(text, 0, PATINDEX('%</head>%', text)), 
                 PATINDEX('%<head>%', text) + 6, 
                 32000)
FROM MyTable


See what I mean?
 
Share this answer
 
Comments
Mohd Wasif 27-Mar-14 1:04am    
Thanks a lot. worked for me. my 5
OriginalGriff 27-Mar-14 5:06am    
You're welcome!
SQL
declare @xml xml;
declare @val varchar(500) = '<head>record1,record2</head>';
set @xml = CONVERT(xml,@val);

select tab.col.value('head[1]','varchar(20)') vallue from @xml.nodes('/') tab(col);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900