1

i would make a script that iterate over the records of a table with a cursor and extract from a column value formatted like that "yyy://xx/bb/147011" only the final number 147011and to put this value in a variable. It's possible to do something like that? Many thanks.

3 Answers 3

3

You don't need a cursor for this. You can just use a query. The following gets everything after the last /:

select right(str, charindex('/', reverse(str)) - 1 )
from (values ('yyy://xx/bb/147011')) v(str)

It does not specifically check if it is a number, but that can be added as well.

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

Comments

1

You can also use the below query.

SELECT RIGHT(RTRIM('yyy://xx/bb/147011'), 
CHARINDEX('/', REVERSE('/' + RTRIM('yyy://xx/bb/147011'))) - 1) AS LastWord

2 Comments

pardon my ignorance, but why it's needed to RTRIM the string?
@Giu It has not any major impact.
1

If numeric value has exact position defined with sample data, then you can do :

SELECT t.*, SUBSTRING(t.col, PATINDEX('%[0-9]%', t.col), LEN(t.col))
FROM table t; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.