比较SQL Server中的iif()中的日期

时间:2012-11-23 15:49:05

标签: sql-server-2008

我正在尝试在SQL Server中使用以下查询

SELECT [AL].[Subscriptions].Id,
       [AL].[Subscriptions].name,
       [AL].[Subscriptions].description,
       [AL].[Subscriptions].price,
       [AL].[Subscriptions].iconFileName,
       IIf(a.expiryDate > Now(), 'TRUE', 'FALSE') AS isSubsByUser
FROM   [AL].[Subscriptions]
       LEFT JOIN (SELECT *
                  FROM   [AL].[UserSubscriptions]
                  WHERE  userId = 13259) AS a
         ON Subscriptions.Id = a.itemid; 

但总是得到错误

  

函数参数列表出错:'>'未识别。   无法解析查询文本。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

就像马丁史密斯所说,你需要使用案例陈述。此外,您似乎只在派生表中使用了几个字段,因此我建议不要使用*。我在下面举了一个例子。

   SELECT [AL].[Subscriptions].Id,
        [AL].[Subscriptions].name,
        [AL].[Subscriptions].description,
        [AL].[Subscriptions].price,
        [AL].[Subscriptions].iconFileName,
        case when a.expiryDate > GetDate() then 'TRUE' else 'FALSE' end AS isSubsByUser
   FROM   [AL].[Subscriptions]
   LEFT JOIN (SELECT expiryDate, itemid 
              FROM   [AL].[UserSubscriptions]
              WHERE  userId = 13259) AS a
     ON Subscriptions.Id = a.itemid;