문제

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