如何替换<p>和</p>标记之间的所有空格

时间:2012-10-19 18:47:57

标签: sql sql-server-2008

我正在浏览大约5000条记录,需要将<p> </p>代码与&nbsp;之间的所有空格替换为

这会有用吗?

我认为这适用于SET replace()

SELECT *
FROM FMS.[AuditItem] ai
WHERE ai.[NotificationMessage] LIKE '%<p>% %</p>%'

但我仍然无法仅选择段落标记之间的空格。对?有没有更好的方法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

首先来看看

字符串可以包含其中一些模式

 sometext<p>sometext</p>sometext
 sometext<p>sometext</p>
 <p>sometext</p>sometext
 <p>sometext</p>
 sometext</p>
 <p>sometext
 sometext

如果你想减少风险,那么应该考虑以前的模式 所以,这就是例子

  declare @mem table(id int identity(1,1), NotificationMessage varchar(80))

  insert into @mem(NotificationMessage)
  select 'that is some text initial<p> one two three four  </p> asdf asdf asdf'

  insert into @mem(NotificationMessage)
  select '<p> one two three four  </p> asdf asdf asdf'

  insert into @mem(NotificationMessage)
  select 'that is some text initial<p> one two three four  </p>'

  insert into @mem(NotificationMessage)
  select '<p> one two three four  </p>'

  insert into @mem(NotificationMessage)
  select 'one two three four  </p>'

  insert into @mem(NotificationMessage)
  select 'one two three four'


  select
  NotificationMessage,
  case 
  when CHARINDEX('<P>',NotificationMessage) > 1  then SUBSTRING(NotificationMessage,1,CHARINDEX('<P>',NotificationMessage)-1)
  else ''
  end as section1,

  REPLACE(SUBSTRING(NotificationMessage,                                                                                                    
                        case when CHARINDEX('<P>' , NotificationMessage) > 0 then
                                    CHARINDEX('<P>' , NotificationMessage) + 3
              else 1
                        end,                                                                                                    
                        case when CHARINDEX('</P>', NotificationMessage) > 0  then
                                    CHARINDEX('</P>' , NotificationMessage)
              else LEN(NotificationMessage) 
                        end 
                        -
                        case when CHARINDEX('<P>' , NotificationMessage) > 0 and CHARINDEX('</P>', NotificationMessage) > 0  then
                                    CHARINDEX('<P>' , NotificationMessage) + 3
              else 1 end) , ' '
   ,'&nbsp;') as section2,

  case 
  when CHARINDEX('</P>',NotificationMessage) > 1 then
   case when len(NotificationMessage) > (CHARINDEX('</P>',NotificationMessage) + 4)  then SUBSTRING(NotificationMessage,CHARINDEX('</P>',NotificationMessage)+4,  len(NotificationMessage) - CHARINDEX('</P>',NotificationMessage)+4 )
   else ''
   end
  else ''
  end as section3,

  case 
  when CHARINDEX('<P>',NotificationMessage) > 1  then SUBSTRING(NotificationMessage,1,CHARINDEX('<P>',NotificationMessage)-1)
  else ''
  end 
  +
  REPLACE(SUBSTRING(NotificationMessage,                                                                                                    
                        case when CHARINDEX('<P>' , NotificationMessage) > 0 then
                                    CHARINDEX('<P>' , NotificationMessage) + 3
              else 1
                        end,                                                                                                    
                        case when CHARINDEX('</P>', NotificationMessage) > 0  then
                                    CHARINDEX('</P>' , NotificationMessage)
              else LEN(NotificationMessage) 
                        end 
                        -
                        case when CHARINDEX('<P>' , NotificationMessage) > 0 and CHARINDEX('</P>', NotificationMessage) > 0  then
                                    CHARINDEX('<P>' , NotificationMessage) + 3
              else 1 end) , ' '
   ,'&nbsp;') 
  +
  case 
  when CHARINDEX('</P>',NotificationMessage) > 1 then
   case when len(NotificationMessage) > (CHARINDEX('</P>',NotificationMessage) + 4)  then SUBSTRING(NotificationMessage,CHARINDEX('</P>',NotificationMessage)+4,  len(NotificationMessage) - CHARINDEX('</P>',NotificationMessage)+4 )
   else ''
   end
  else ''
  end as newstring
  from @mem

答案 1 :(得分:0)

如果列中有多个p标签集,那么这只会抓住第一个,其余的将被删除。如果那没关系那么这应该可以解决问题

UPDATE FMS.[AuditItem]
SET    [NotificationMessage] = REPLACE(SUBSTRING([NotificationMessage]
                                                  ,CHARINDEX('<P>' , [NotificationMessage]) + 3
                                                  ,CHARINDEX('</P>', [NotificationMessage]) - CHARINDEX('<P>', [NotificationMessage]) - 3)
                                       , ' '
                                       ,'&nbsp;')
WHERE  [NotificationMessage] LIKE '%<P>%' AND [NotificationMessage] LIKE '%</P>%'