Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to select the last characters from a string?

$
0
0

I have the following string and am trying to select only the characters before the last "</>".  How can I do this?

declare @String varchar(500)

set @String = '<p>Assessed By: Michael Jordan Yagnesh</p>
<p>Reviewed By: Fred Smith</p>
<p>Home Address</p>'

select REVERSE(substring(REVERSE(@String),5,charindex(':',REVERSE(@String))-5))

Here is what I tried so far:

SELECT SUBSTRING(@String,CHARINDEX('</p>',@String,(CHARINDEX('</p>',@String)+1))-11,
             LEN(@String)-CHARINDEX('</p>',@String)-20)


SELECT RIGHT(@String,CHARINDEX('</p>',REVERSE(@String),0)-1)

select LEN(@String)
select charindex('Reviewed By: ',(@String))
select charindex('Reviewed By: ',Reverse(@String)) + datalength('</p>')+1,len(@String)
select SUBSTRING(@String, 61,10)


Ryan D



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>