0

I have found many answers that have pointed me on the right track for what I want, like this.

However, if I had a string like "this attachment will be 22 in. x 15 in. long" I would want to grab "22" (the first int value in the string). Also, I won't know how large (# of characters) the int value will be so I'll need to find the WHOLE first int value until the next special character/space in the string.

Any idea how I can go about doing this?

1
  • 4
    possible duplicate of SQL Extract substring Commented Sep 22, 2015 at 20:07

1 Answer 1

3

Assuming there really is a number in the string, you can use patindex():

select left(s, patindex('%[^0-9]%', s) - 1)
from (select substring(col, patindex('%[0-9]%', col), len(col)) as s
      from t
     ) t;
Sign up to request clarification or add additional context in comments.

2 Comments

That's it! However, I won't know the length argument in the substring syntax and would need the code to calculate that as well. Basically, I want it to grab the first WHOLE int (and possible decimal) value until the next special character/value (spaces, colons, semi-colons, hyphens, etc.) but ignore periods too so I can use decimal values.
Figured it out! Just simply had to use the LEN function on top of the PATINDEX function for the Length argument select SUBSTRING(col, patindex('%[0-9]%', col), len(patindex('%[0-9]%', col))) as s from t. Thanks for all your help, Gordon! :)

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.