سؤال

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
هل كانت مفيدة؟

المحلول

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

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

نصائح أخرى

One way to handle mm/''/null;

select cast(isnull(replace('0' + @createNumber, 'mm', ''), 0) as decimal(6,3))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top