2014-08-27 66 views
0

對於我的項目,我們使用的是MS SQL 2008.在子查詢中,我喜歡在WHERE子句中搜索當前(在SQL循環中)年的最後一天。它應該是這樣的:SQL WHERE date部分字段和部分預定義字符串

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

怎麼樣嘗試這種方式'WHERE年(datefieldsubquery) 2014-08-27 11:46:07

+1

你的數據類型是什麼? datefieldsubquery是日期嗎?約會時間? VARCHAR? currentyearparentquery是一個整數嗎? VARCHAR?你問的是可能的,但我們需要更多的細節來幫助你。 – 2014-08-27 11:52:11

+0

什麼是currentyearparentquery在您的查詢? – 2014-08-27 11:55:02

回答

1

WHERE條款

WHERE datefieldsubquery = Convert(varchar(50),YEAR(GETDATE()))+'-12-31 23:59:00' 
+0

此解決方案適合我!字符串會話是解決方案。 – JelleP 2014-08-27 12:03:13