Strip HTML Tags

  • dbishop (10/7/2015)


    I know it has been quite some time since this was posted, but I have a question. The code is looking for an EXACT match to the start tag. However, if you have additional info (e.g. <span style=...> or <div class=...> and </span> or </div> you get a parsing error.

    Is there any way to avoid that? I've found some code that parses character by character but we know that is, like 🙁

    I know it's not the answer you wanted but the absolute best way would be to have the app that spawned it to parse it and provide it in a more normal form.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • I recently had the same requirement and wrote a function which strips HTML tags and entities regardless of any additional information. I posted the code here at Stack Overflow

    I hope it is of help to someone.

    Garrie Powers
    IT Developer
    Comprehensive Clinical Trials Unit at UCL
    Institute of Clinical Trials and Methodology

  • g.powers (10/21/2016)


    I recently had the same requirement and wrote a function which strips HTML tags and entities regardless of any additional information. I posted the code here at Stack Overflow

    I hope it is of help to someone.

    You should look at the solutions posted in this thread. Yours is a good solution but the performance is going to be pretty painful. You have a scalar function with a while loop. The solutions posted here are set based and will be crazy fast in comparison.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Really stupid question, how do I apply the function? I need to strip html from one column in a table (rq_req_comment).

    FROM [test_domain_dgps_export_db].

    .[REQ] r

    where r.rq_req_comment like '<html>%');

    exec dbo.udf_striphtml @HTMLText

    ]
    I get an error:
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    Thanks

  • barry.l.daniel - Thursday, February 22, 2018 12:26 PM

    Really stupid question, how do I apply the function? I need to strip html from one column in a table (rq_req_comment).

    FROM [test_domain_dgps_export_db].

    .[REQ] r

    where r.rq_req_comment like '<html>%');

    exec dbo.udf_striphtml @HTMLText

    ]
    I get an error:
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    Thanks

    The problem isn't the function it is your code. When you use SET to assign a variable and use a query to get the value that error is returned when the query returns more than 1 row. In other words you have more than 1 row in test_domain_dgps_export with a comment that starts with <html>. How could the query engine know which row you want?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply