SQL WHERE日期部分字段和部分预定义字符串

时间:2014-08-27 11:44:13

标签: sql sql-server-2008

对于我的项目,我们使用的是MS SQL 2008.在子查询中,我喜欢在WHERE子句中搜索当前(在SQL循环)年份的最后一天。它应该是这样的:

WHERE datefieldsubquery = currentyearparentquery+'-12-31 23:59:00'

但是使用它时子查询中没有输出。例如,查询是:

WHERE datefieldsubquery = '2014-12-31 23:59:00'

查询返回结果。我喜欢'2014'是从父查询动态继承的。这可能在SQL中吗?

- 编辑 - 这两个字段都是日期时间。

真正的完整查询:

   SELECT factuurregel.[Internal costkind] AS code,
    COUNT(factuurregel.[Internal costkind]) AS total,
    YEAR(factuurregel.[Invoice date lease company]) AS maand,                       
    (
      SELECT COUNT(leasecontract.[Contract Ending Date]) AS autos 
      FROM [test$Lease Contract] AS leasecontract                   
      WHERE (leasecontract.[Status] = '0' OR leasecontract.[Status] = '1') 
      AND YEAR(leasecontract.[Contract Activation Date]) <= YEAR(factuurregel.[Invoice date lease company])
      AND (YEAR(leasecontract.[Contract Ending Date]) > YEAR(factuurregel.[Invoice date lease company]) 
      OR leasecontract.[Contract Ending Date] = '1753-01-01 00:00:00')                                                  
    ) AS autos
    FROM [test$Invoice line] AS factuurregel
    LEFT JOIN [test$Lease Car] AS leasecar ON factuurregel.[License No_] = leasecar.[License No_]
    WHERE factuurregel.[Internal costkind] >= '200' 
    AND factuurregel.[Internal costkind] < '300'
    AND (leasecar.[Licence Type] = 1 OR leasecar.[Licence Type] = 2)
    GROUP BY YEAR(factuurregel.[Invoice date lease company]),factuurregel.[Internal costkind]
    ORDER BY factuurregel.[Internal costkind]

1 个答案:

答案 0 :(得分:1)

WHERE条款

中尝试这种方式
WHERE datefieldsubquery = Convert(varchar(50),YEAR(GETDATE()))+'-12-31 23:59:00'