Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove trailing chars to get numeric value

I'm struggling to combine two expression into one so that I can remove trailing 'mm' chars of a varchar column which holds values like 3.214mm and use those chars as numeric values.

The problem I have is that there can be also null or empty string values and I can't find a way to combine both expressions below.

Example: SQLfiddle

DECLARE @string varchar(128)
SET @string = '4.123mm'
SELECT ISNULL(NULLIF(@string,''),NULL) As MyString ;

DECLARE @createNumber varchar(128)
SET @createNumber = '4.123mm'
select LEFT(@createNumber, NULLIF (LEN(@createNumber) - 2, - 1))As MyNumber
like image 711
Tony Clifton Avatar asked Apr 28 '26 19:04

Tony Clifton


1 Answers

DECLARE @createNumber varchar(128)
SET @createNumber = ''
select reverse(stuff(reverse(@createNumber), 1,2, ''))

This will return null if createnumber is shorter than 2 characters.

like image 168
t-clausen.dk Avatar answered Apr 30 '26 11:04

t-clausen.dk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!