1

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.

1 Answer 1

2

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

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

Example at SQL Fiddle.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. It worked. In my e.g. above, I put 20 becuase I thought form Name would not be that long. But how do I make sure I get exact formName? no spaces or blanks after exact formName? FormName's length varies.
You could add rtrim to remote spaces after the formname
will that be same to this:(substring(FormName, charindex(', ', FormName, 0) + 2, LEN(FornName)) ?
I m not comfy with fixed number 20 in my query. So I thought of getting exact length. Is this ok?
The exact length is fine. Note that len() returns the length without trailing spaces, f.e. len(' ') = 0
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.