开发者

UPDATE and REPLACE MS Office formatting with SQL

I am trying to rid a column of the additional tags MS Office provides 开发者_运维知识库when you paste content from Word onto a form on my website.

How do I update and replace occurrences of the content inside (and including) the and ?

Here's what I've got so far:

UPDATE TABLE
SET myColumn = REPLACE(myColumn, SUBSTRING(myColumn, CHARINDEX('<!--[if gte mso', myColumn), CHARINDEX('<![endif]-->', myColumn)-1), '')

My plan is to execute this query a few times until all occurrences are gone from the column. However, it's when the column doesn't have any occurrences I run into problems.

I'm guessing it's because of the way I calculate the length in the Substring... Everytime I execute a few characters are removed from the beginning of the column.

Do I need something like a case statement to skip the rows that don't have the MS Office stuff?

Thanks,


As a hacky update you could;

 create table test(id int, myColumn varchar(500), newValue varchar(500) null)
 insert test values  
    (1, '<!--[if gte mso XXX>hello<![endif]-->', ''),
    (2, 'aaaaaaa', ''),
    (3, '123<!--[if gte mso XXX>hello<![endif]-->456', ''),
    (4, 'AA<!--[if gte mso 111>222<![endif]-->BB<!--[if gte mso 333>444<![endif]-->CC', '')

;with cte(id, stripped) as (
    select id, myColumn from test
    union all 
    select id, cast(stuff(stripped, charindex('<!--[if gte mso', stripped), charindex('<![endif]-->', stripped)-charindex('<!--[if gte mso', stripped)+len('<![endif]-->'), '') as varchar(500)) 
    from cte
    where charindex('<!--[if gte mso', stripped) > 0
)
update test 
    set test.newValue = cte.stripped
from test   
    inner join cte on cte.id = test.id
    where charindex('<!--[if gte mso', stripped) = 0

select * from test

>>
id  newValue
1   
2   aaaaaaa
3   123456
4   AABBCC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜