Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL how to extract middle of string

I want to only get the unit number out of this string:

'<p>The status for the Unit # 3546 has changed from % to OUTTOVENDOR</p>'

The note is always exactly the same but the unit number changes. How do I extract just the unit number?

Thanks!

like image 972
user380432 Avatar asked Sep 09 '10 16:09

user380432


2 Answers

declare @String varchar(500)

set @String = '<p>The status for the Unit # 3546 has changed from % to OUTTOVENDOR</p>'

select SUBSTRING(@String, charindex('# ', @String) + 2, charindex('has changed', @String) - charindex('# ', @String) - 2)
like image 133
Joe Stefanelli Avatar answered Oct 09 '22 06:10

Joe Stefanelli


try:

  declare @S VarChar(1000)
  Set @S = 
  '<p>The status for the Unit # 3546 has changed from % to OUTTOVENDOR</p>'
  Select Substring( @s, 
         charIndex('#', @S)+1, 
         charIndex('has', @S) - 2 - charIndex('#', @S)) 
like image 24
Charles Bretana Avatar answered Oct 09 '22 06:10

Charles Bretana