将CASE结果与另一个CASE SQL命令进行比较 - Crystal Reports

时间:2014-10-28 17:55:38

标签: sql crystal-reports sqlcommand

我试图结合比较一个案例的结果来清理一些数据。对于

SELECT 
CASE 
    WHEN ISNUMERIC(SUBSTRING(LTRIM(RTRIM(X)), 1,1)) = 0
    THEN UPPER(LTRIM(RTRIM(SUBSTRING(X, CHARINDEX(' ', X)+1, LEN(X) - CHARINDEX(' ', X)))))
    ELSE X
END XCU
CASE 
     WHEN XCU LIKE '%A%' OR '%B%' THEN 'Y'
     ELSE X
   END 
FROM XTable

第一个案例是完美的yeilds XCU,然后我想比较XCU找到关键词并得出一般的Y

1 个答案:

答案 0 :(得分:0)

简单的方法是使用嵌套的SQL查询

SELECT CASE 
         WHEN xcu LIKE '%A%' 
               OR xcu like '%B%' THEN 'Y' 
         ELSE 'x' 
       END, 
       xcu 
FROM  (SELECT CASE 
                WHEN Isnumeric(Substring(Ltrim(Rtrim(x)), 1, 1)) = 0 THEN Upper( 
                Ltrim(Rtrim(Substring(x, Charindex(' ', x) + 1, Len(x) - 
                            Charindex(' ', x) 
                            )))) 
                ELSE 'x' 
              END XCU 
       FROM   xtable)  A