get middle value from a string

2.2k Views Asked by At

let say i've got a list of values, with such a format:

BookIndex - value what i need - random stuff

BookIndex - B 12x - AXD39
BookIndex - A 99a - PZT00

...

Question: how to get value what i need with as little functions as possible?

Output

B 12x
A 99a
2

There are 2 best solutions below

2
On

answer based on subject of your question: SUBSTRING(FieldName, StartIndex, Length)

any general answer? because there are no static length of value that i want.

Find StartIndex and End index. In your samples you need text between char '-'

select SUBSTRING(TextData, CHARINDEX('-', TextData), CHARINDEX('-', TextData, CHARINDEX('-', TextData) + 1)) as E1 from [Untitled - 1] 
0
On
select substring(T.stringbehinddash
                 ,0,charindex('-',T.stringbehinddash) - 1)
from
(select substring(test,charindex('-',test) + 1 ,len(test))
 as stringbehinddash
 from mytable) T

SQLFIDDLE