2

私は約5000のレコードを調べており、<p> </p>タグ間のすべてのスペースを次のように置き換える必要があります。&nbsp;

これは機能しますか?

これはSETreplace()で機能すると思いました

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 に答える