2013-07-24 73 views
0

我想選擇所有列,但是如果fuel = 1,那麼開始日期>'oct oct 2013'else else if fuel = 0開始日期>'9月1日'。SQL查詢:如果x = 1,則選擇全部,然後y <z否則如果x = 2,那麼y <w

使用SQL Server 2005

我想這個..

SELECT * FROM EntitlementEpisode 
WHERE 
    IF EnFuel = 1 
     THEN 
      IF 
       BEGIN 
        [EnEpisodeStart] > 
        (SELECT TOP 1 [BeYearStart] 
        FROM [BenefitYear] ORDER BY [BeId] desc) 
       END 
      ELSE IF EnFuel = 0 
     THEN 
      IF 
       BEGIN 
         [EnEpisodeStart] > 
         CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE())) 
      END 

這是要去了解它的正確方法?

回答

3

使用二元運算符的強度...!

SELECT * 
FROM EntitlementEpisode 
WHERE 
    (EnFuel=1 AND EnEpisodeStart] > 
       (SELECT TOP 1 [BeYearStart] 
       FROM [BenefitYear] ORDER BY [BeId] desc)) 

    OR 

    (EnFuel=0 AND [EnEpisodeStart] > 
        CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE()))) 
0

如何像

SELECT * 
FROM EntitlementEpisode 
WHERE (EnFuel = 1 AND [EnEpisodeStart] > (SELECT TOP 1 [BeYearStart] FROM [BenefitYear] ORDER BY [BeId] desc) 
OR  (EnFuel = 0 AND [EnEpisodeStart] > CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE()))) 
相關問題