2

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

这行得通吗?

我认为这适用于 SET replace()

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

但是我仍然没有办法只选择段落标签之间的空格。正确的?有没有更好的方法来解决这个问题?

4

2 回答 2

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
于 2012-10-19T22:49:58.277 回答
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>%'
于 2012-10-19T21:13:10.337 回答