如何使用类似
的min-width
语句
case
没有抛出转换错误?我正在处理的列是CASE
WHEN val IS STRING
THEN something
WHEN val > 0
THEN something
,但它将包含“NO VALUE”或数字。
答案 0 :(得分:1)
这个怎么样......这有帮助
DECLARE @VAL VARCHAR(8)
SELECT @VAL = COLUMNNAME1 FROM TABLENAME1 WHERE SOMEOTHERCOL2 = SOMEVALUE;
SELECT
CASE
WHEN @VAL = 'NO VALUE' THEN NULL
WHEN CAST(@VAL AS BIGINT) > 0 THEN @VAL
ELSE 'LT 1'
END
FROM TABLE
WHERE COLUMNNAME1 = SOMEVALUE
答案 1 :(得分:0)
首先使用CASE&CAST'然后过滤掉导致异常的值。请记住,如果某些字符串不是' NO VALUE'
,这仍然会失败SELECT CASE(so.val)
WHEN 'NO VALUE'
THEN null
ELSE CAST(so.val AS int)
END from dbo.SO41270497 so
答案 2 :(得分:0)
尝试:
DECLARE @myTable TABLE (Val varchar(10))
INSERT INTO @myTable VALUES ('No Value'),('1'),('2'),('3'),('4'),('5')
SELECT * FROM @myTable
SELECT CASE WHEN ISNUMERIC(Val) = 1 THEN CONVERT(int,Val) ELSE NULL END
AS Val FROM @myTable