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:
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?
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.
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With