0

I have string something like ACGI SHIPPING (3471) and I need to get 3471 number out of it.

If there is no number just print 0, in SQL Server

2
  • Is that the only pattern? Commented Nov 5, 2015 at 16:41
  • yes only this pattern Commented Nov 5, 2015 at 16:43

1 Answer 1

1

Here is a solution:

DECLARE @s VARCHAR(100) = 'ACGI SHIPPING (3471)'
SELECT CASE WHEN PATINDEX('%[0-9]%', @s) > 0 
            THEN REPLACE(SUBSTRING(@s, PATINDEX('%[0-9]%', @s), LEN(@s)), ')', '') 
            ELSE 0 END

You are searching for first digit and take substring from that position to end. And then replace ')' with blank.

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

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.