Chad
Chad

Reputation: 24679

I need a T-SQL script that strips the numeric chars on the right

For example

Input: 0123BBB123456  Output: 123456
Input: ABC00123       Output: 00123
Input: 123AB0345      Output: 0345

In other words, the code should start stripping characters from the right and stop when a character that is no 0-9 is encountered.

I have to run this agains several millions of records, so I am looking for an efficient set based approach, not a cursor approach that performs substring functions in a loop for each record.

I am having issues trying to format this for reading. Give me a few minutes.

Frustrating...I think that the browser that I am using, IE6 (mandated by my company) is making this challenging. This site doesnt work well with 6.

Upvotes: 2

Views: 62

Answers (1)

Alex K.
Alex K.

Reputation: 175748

How about;

;with test(value) as (
    select '0123BBB123456' union
    select 'ABC00123' union
    select '123AB0345' union
    select '123'
 )
 select
    value, 
    right(value, patindex('%[^0-9]%', reverse('?' + value)) - 1)
 from test  


0123BBB123456   123456
123             123
123AB0345       0345
ABC00123        00123

Upvotes: 3

Related Questions