2013-10-17 72 views
1

我無法弄清楚如何在matlab中將日期傳遞給我的sql查詢。當我這樣做是 「靜態」,它workds非常清楚:將日期傳遞給matlab中的sql查詢

myquery1 = ['Select DeliveryMonth, Value '... 
' FROM [mydatabase] '... 
' where idcurve = 33 ' ... 
' and deliverymonth <''20121130'' '... 
    ' order by DeliveryMonth '] 

但我想是這樣的:

breakdate = input('Enter a breakdate as 20121130: ', 's') 

myquery1 = ['Select DeliveryMonth, Value '... 
' FROM [mydatabase] '... 
' where idcurve = 33 ' ... 
' and deliverymonth < ''breakdate'' '... 
    ' order by DeliveryMonth '] 

問候 一個

回答

0
' and deliverymonth < ' + breakdate + ... 
+0

對不起,不起作用。 ahve試過這個:'and deliverymonth'''''breakdate'''...它不會給出任何錯誤,但也不會返回任何數據... – oceanfront

1

你缺少一個單引號:

breakdate = '20121130' 
myquery1 = ['Select DeliveryMonth, Value '... 
' FROM [mydatabase] '... 
' where idcurve = 33 ' ... 
' and deliverymonth < '''breakdate''' '... 
    ' order by DeliveryMonth '] 

返回:

myquery1 = 

Select DeliveryMonth, Value FROM [mydatabase] where idcurve = 33 and deliverymonth < '20121130' order by DeliveryMonth 
+0

檢查myquery1它只是退縮... deliverymonth <'breakdate '...某處存在一些轉換問題。 – oceanfront

+0

好吧,所以我通過使用來自mathworks的一個偉大的GUI解決了這個問題,網址是http://www.mathworks.com/matlabcentral/fileexchange/8313-uigetdate/content/uigetdate.m,然後將我的代碼更改爲myquery1 = ['Select DeliveryMonth ,'Value'... 'FROM [ENOI_INDICI]。[dbo]。[v_mdm_MCurves]'... 'where idcurve = 33'... 'and deliverymonth <','''',datestr(breakdate, 29),''''... 'order by DeliveryMonth'] – oceanfront