Nemo
Nemo

Reputation: 1111

SQL parse string

I have SQL column in this format:

http://myServer/Lists/myform/DispForm.aspx?ID=6, RAB12EGH234
http://myServer/Lists/myform/DispForm.aspx?ID=5, CBTRR2345

I want to get only

RAB12EGH234
CBTRR2345

I used this:

select 
   substring([FormName], charindex(',', ([FormName]), 0), 20) 
from [myDB].[dbo].[FormList]

However I get results like:

 , RAB12EGH234
 , CBTRR2345

I don't want any space, I don't want comma but just the names. How do I edit my query? Thanks.

Upvotes: 1

Views: 200

Answers (1)

Andomar
Andomar

Reputation: 238086

Skip two extra characters, and use rtrim() to remove trailing spaces:

rtrim(substring(FormName, charindex(', ', FormName, 0) + 2, 20))
                                               ^^^^^^

Example at SQL Fiddle.

Upvotes: 2

Related Questions