How to replace html tags in string using a function

  1. CREATE FUNCTION [dbo].[fn_parsehtml]   
  2. (  
  3. @htmldesc varchar(max)  
  4. )   
  5. returns varchar(max)  
  6. as  
  7. begin  
  8. declare @first int, @last int,@len int   
  9. set @first = CHARINDEX('<',@htmldesc)   
  10. set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc))   
  11. set @len = (@last - @first) + 1   
  12. while @first > 0 AND @last > 0 AND @len > 0   
  13. begin   
  14. ---Stuff function is used to insert string at given position and delete number of characters specified from original string  
  15. set @htmldesc = STUFF(@htmldesc,@first,@len,'')    
  16. SET @first = CHARINDEX('<',@htmldesc)   
  17. set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc))   
  18. set @len = (@last - @first) + 1   
  19. end   
  20. return LTRIM(RTRIM(@htmldesc))   
  21. end