Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've got a VARCHAR variable of this kind:

@string = "BLABLA10;GOGOGO30;RES777;ANOTHER;"

(several keyword + number separated by ;)

I need to get the INT value after "RES" in a SQL query (on SYBASE) ! (lol -_-)

I thought it would be always 3 characters, so I did:

SELECT @val = CONVERT(INT, LTRIM(RTRIM(SUBSTRING(@string, CHARINDEX(";RES", @string)+4, 3))))

But now, it can be 2 characters (or more) for example... so I need to get the value between RES and the next ;

I've done a complicated query:

CONVERT(INT, LTRIM(RTRIM(SUBSTRING(@string, CHARINDEX(";RES", @string)+4, -4 + CHARINDEX(";", SUBSTRING(@string, CHARINDEX(";RES", @string)+1, 70) )))))

It works but it's horrible to read.

Has anyone an idea for a more readable way than this?

I know that the real problem is that we should not use value in concatenated string, but well it was done that way and I can't change it.

share|improve this question
    
Sadly, that looks about right. You could hide it away in a UDF, but performance would suffer. –  RubberDuck yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.