我正在使用CASE
命令将字符串查询结果从数字更改为名称。
事情似乎工作正常,但我很难弄清楚为什么结果只显示字符串的第一个值。
Declare @OverrideON INT, @OverrideOFF INT
SET @OverrideON = 1 SET @OverrideOFF = 0
SELECT LonDeviceName,
CASE WHEN SUBSTRING (PointValue,65,1) = @OverrideON THEN 'Fan In Override'
WHEN SUBSTRING (PointValue,67,1) = @OverrideON THEN 'HW In Override'
WHEN SUBSTRING (PointValue,69,1) = @OverrideON THEN 'CHW In Override'
WHEN SUBSTRING (PointValue,71,1) = @OverrideON THEN 'OAD In Override'
WHEN SUBSTRING (PointValue,73,1) = @OverrideON THEN 'VFD In Override'
WHEN SUBSTRING (PointValue,65,1) = @OverrideOFF THEN 'Fan Normal'
WHEN SUBSTRING (PointValue,67,1) = @OverrideOFF THEN 'HW Normal'
WHEN SUBSTRING (PointValue,69,1) = @OverrideOFF THEN 'CHW Normal'
WHEN SUBSTRING (PointValue,71,1) = @OverrideOFF THEN 'OAD Normal'
WHEN SUBSTRING (PointValue,73,1) = @OverrideOFF THEN 'VFD Normal'
ELSE '**StringError**'
END as 'Manual Overrides'
FROM dbo.Points
WHERE LogicName like '%override%'
当代码运行时,它会创建一个标题为Manual Overrides
的列,该列正确,在该列中显示字符串中的第一个值。但是,字符串中其他四个值的结果在哪里。任何帮助或想法将不胜感激。
感谢。
答案 0 :(得分:1)
这样的事情怎么样:
SELECT LonDeviceName,
CASE WHEN SUBSTRING (PointValue,65,1) = @OverrideON THEN 'In Override'
WHEN SUBSTRING (PointValue,65,1) = @OverrideOFF THEN 'Normal'
ELSE '**StringError**'
END as 'Fan',
CASE WHEN SUBSTRING (PointValue,67,1) = @OverrideON THEN 'In Override'
WHEN SUBSTRING (PointValue,67,1) = @OverrideOFF THEN 'Normal'
ELSE '**StringError**'
END as 'HW',
CASE WHEN SUBSTRING (PointValue,69,1) = @OverrideON THEN 'In Override'
WHEN SUBSTRING (PointValue,69,1) = @OverrideOFF THEN 'Normal'
ELSE '**StringError**'
END as 'CHW',
CASE WHEN SUBSTRING (PointValue,71,1) = @OverrideON THEN 'In Override'
WHEN SUBSTRING (PointValue,71,1) = @OverrideOFF THEN 'Normal'
ELSE '**StringError**'
END as 'OAD',
CASE WHEN SUBSTRING (PointValue,73,1) = @OverrideON THEN 'In Override'
WHEN SUBSTRING (PointValue,73,1) = @OverrideOFF THEN 'Normal'
ELSE '**StringError**'
END as 'VFD'
FROM dbo.Points
WHERE LogicName like '%override%'
答案 1 :(得分:0)
你真的需要这些庞大的变量来代表1和0吗?真?无论如何,真的猜测你想要输出什么。也许这个:
SELECT LonDeviceName, [Manual Overrides] = ''
+ 'Fan ' + CASE SUBSTRING (PointValue,65,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ ',HW ' + CASE SUBSTRING (PointValue,67,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ ',CHW ' + CASE SUBSTRING (PointValue,69,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ ',OAD ' + CASE SUBSTRING (PointValue,71,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ ',VFD ' + CASE SUBSTRING (PointValue,73,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
FROM dbo.Points
WHERE LogicName LIKE '%override%';
或者您可能只想查看实际覆盖的值:
SELECT LonDeviceName, [Manual Overrides] = ''
+ 'Fan ' + CASE SUBSTRING (PointValue,65,1)
WHEN '1' THEN 'In Override' ELSE '' END
+ ',HW ' + CASE SUBSTRING (PointValue,67,1)
WHEN '1' THEN 'In Override' ELSE '' END
+ ',CHW ' + CASE SUBSTRING (PointValue,69,1)
WHEN '1' THEN 'In Override' ELSE '' END
+ ',OAD ' + CASE SUBSTRING (PointValue,71,1)
WHEN '1' THEN 'In Override' ELSE '' END
+ ',VFD ' + CASE SUBSTRING (PointValue,73,1)
WHEN '1' THEN 'In Override' ELSE '' END
FROM dbo.Points
WHERE LogicName LIKE '%override%';
或许你想要单独的列:
SELECT LonDeviceName,
+ [Fan] = CASE SUBSTRING (PointValue,65,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ [HW] = CASE SUBSTRING (PointValue,67,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ [CHW] = CASE SUBSTRING (PointValue,69,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ [OAD] = CASE SUBSTRING (PointValue,71,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
+ [VFD] = CASE SUBSTRING (PointValue,73,1)
WHEN '1' THEN 'In Override' ELSE 'Normal' END
FROM dbo.Points
WHERE LogicName LIKE '%override%';
或许完全不同的东西。如果我们看不到源数据或您想要的结果,那么很难将您编写的查询与您想要的结果之间的差距缩小。