まず見てください
文字列には、これらのパターンの一部を含めることができます
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) , ' '
,' ') 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) , ' '
,' ')
+
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