Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove any trailing numbers from a string?

Sample inputs:

"Hi there how are you"

"What is the #1 pizza place in NYC?"

"Dominoes is number 1"

"Blah blah 123123"

"More blah 12321 123123 123132"

Expected output:

"Hi there how are you"

"What is the #1 pizza place in NYC?"

"Dominoes is number"

"Blah blah"

"More blah"

I'm thinking it's a 2 step process:

  1. Split the entire string into characters, one row per character (including spaces), in reverse order
  2. Loop through, and for each one if it's a space or a number, skip, otherwise add to the start of another array.

And i should end up with the desired result.

I can think of a few quick and dirty ways, but this needs to perform fairly well, as it's a trigger that runs on a busy table, so thought i'd throw it out to the T-SQL pros.

Any suggestions?

like image 933
RPM1984 Avatar asked Feb 10 '12 02:02

RPM1984


People also ask

How do you remove trailing numbers in Python?

To remove trailing zeros from a decimal:Use the str() class to convert the decimal to a string. Use the str. rstrip() method to strip the trailing zeros if the number has a decimal point.

How do I remove all characters from a string except numbers?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

How to Remove numbers from string using regex Python?

In Python, an inbuilt function sub() is present in the regex module to delete numbers from the Python string. The sub() method replaces all the existences of the given order in the string using a replacement string.

How do you remove the last number in a string in Java?

replaceAll("\\d*$", ""); This should match any number of trailing digit characters (0-9) that come at the end of the string and replace them with an empty string.


1 Answers

This solution should be a bit more efficient because it first checks to see if the string contains a number, then it checks to see if the string ends in a number.

 CREATE FUNCTION dbo.trim_ending_numbers(@columnvalue AS VARCHAR(100)) RETURNS VARCHAR(100)
    BEGIN
    --This will make the query more efficient by first checking to see if it contains any numbers at all
    IF @columnvalue NOT LIKE '%[0-9]%'
        RETURN @columnvalue

    DECLARE @counter INT
    SET @counter = LEN(@columnvalue)

    IF ISNUMERIC(SUBSTRING(@columnvalue,@counter,1)) = 0
        RETURN @columnvalue 

    WHILE ISNUMERIC(SUBSTRING(@columnvalue,@counter,1)) = 1 OR SUBSTRING(@columnvalue,@counter,1) = ' '
    BEGIN
        SET @counter = @counter -1
        IF @counter < 0
            BREAK
    END
    SET @columnvalue = SUBSTRING(@columnvalue,0,@counter+1)

    RETURN @columnvalue
    END

If you run

SELECT dbo.trim_ending_numbers('More blah 12321 123123 123132')

It will return

'More blah'
like image 107
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Avatar answered Oct 16 '22 03:10

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz