2012-07-25 84 views
0

我正在使用CASE命令將子字符串查詢結果從一個數字更改爲名稱。SQL Server 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列這是正確的,在該列中它顯示的字符串中的第一個值。但是,字符串中其他四個值的結果在哪裏。任何援助或想法將不勝感激。

謝謝。

+5

CASE不是一個語句,它是一個返回單個值的表達式。它永遠不會從你的字符串中返回2個或更多的值。你可以展示一個簡化版本的數據和期望的結果嗎? – 2012-07-25 20:48:01

+0

爲什麼不創建ManualOverrides表,以便每次獲取新表時不必修改此查詢? – Bert 2012-07-25 20:51:40

+0

你需要連接在同一行還是四行不同的值嗎? – Lamak 2012-07-25 20:54:05

回答

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%' 
+0

謝謝@RedFilter這完全按照我的計劃工作。 99%在這裏完成,現在我必須將where子句更改爲僅顯示「In Override」中的行。再次感謝!! – Kris 2012-07-26 07:50:20

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%'; 

也許完全不同的東西。如果我們無法看到源數據或您想要的結果,那麼填寫您寫入的查詢與您想要的結果之間的差距是很困難的。

+0

這也適用。謝謝... – Kris 2012-07-26 07:45:37

相關問題