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.