0

块引用

DECLARE @Symbol nvarchar(50);
DECLARE @EnDate1 Datetime;
DECLARE @StDate1 Datetime;
SET @Symbol = 'RIM12f'
SET @EnDate1 = '05/08/2012 9:00'
SET @StDate1 = '05 / 07 / 2012 12 : 00'
SELECT  *
FROM   Siri
WHERE (DATEADD(D, 0, DATEDIFF(D, 0, Date)) BETWEEN @StDate1 AND @EnDate1)
  AND  CASE Symbol = WHEN @Symbol = 'ALL'
                     THEN Symbol
                     ELSE Symbol LIKE LEFT(@Symbol, len(@Symbol) - 3) + '%'
       END

它不一样,但它可以与 else @Symbol 一起使用,但我需要所有符号,如 LEFT(@Symbol, len(@Symbol) - 3) + '%' if @Symbol 不是所有

4

3 回答 3

2

修改你的案例表达式:

 Symbol LIKE CASE @Symbol WHEN 'ALL'
                     THEN Symbol
                     ELSE LEFT(@Symbol, len(@Symbol) - 3) + '%'
于 2012-06-30T14:16:49.847 回答
0
DECLARE @Symbol nvarchar(50); 
DECLARE @LikeSymbol nvarchar(50); 

DECLARE @EnDate1 Datetime; 
DECLARE @StDate1 Datetime; 
SET @Symbol = 'RIM12f' 
SET @EnDate1 = '05/08/2012 9:00' 
SET @StDate1 = '05 / 07 / 2012 12 : 00' 
SET @LikeSymbol = LEFT(@Symbol, length(@Symbol) - 3) + '%'
SELECT  * 
FROM   Siri 
WHERE (DATEADD(D, 0, DATEDIFF(D, 0, Date)) BETWEEN @StDate1 AND @EnDate1) 
AND (
(@Symbol = 'All')
OR
(Symbol Like @LikeSymbol)
)

猜测是您查询的意图还远不清楚

于 2012-06-30T14:17:21.900 回答
0

你不能CASE同时拥有=和嵌套LIKE..你可以使用 LIKE 或使用ORAND构造..(如建议的@Tony Hopkinson)

这是修改后的案例:

Symbol like CASE WHEN @Symbol = 'ALL'
                     THEN Symbol
                     ELSE LEFT(@Symbol, len(@Symbol) - 3) + '%'
END
于 2012-06-30T14:29:52.330 回答