2015-11-05 92 views
1

我能夠生成一個查詢,它會給我10月份的自動保存。sql oracle當前月份

但是,我試圖修改它,所以我可以有一個通用的當前月份,所以我不必每次都更新查詢。

任何想法?

下面我查詢10月,2015年

SELECT 
c.invoicenum,c.invoicedate, c.vendorname,c.account, c.invoiceid , decode(nvl(b.invoiceid,-1),-1,'No','Yes') "Approved", 
decode(nvl(b.invoiceid,-1),-1,'No',nvl(a.invoiceid,-2),'No','Yes') "Auto Saved for Approval" 
FROM 
(SELECT d.invoicenum,d.invoicedate, v.vendorname,d.account, d.invoiceid FROM invoice d, vendor v 
WHERE d.invoicestatus in (1,5,7,8) AND v.vendorid=d.vendorid AND v.status=0 AND to_char(d.dateentered,'MMYY')='1015') c, 

(SELECT d.invoicenum,d.invoicedate, v.vendorname,d.account, d.invoiceid FROM invoice d, vendor v 
WHERE d.invoicestatus in (7,8) AND v.vendorid=d.vendorid AND v.status=0 AND to_char(d.dateentered,'MMYY')='1015') b, 

(SELECT d.invoicenum,d.invoicedate, v.vendorname,d.account, d.invoiceid FROM invoicehistory b, invoice d, vendor v 
WHERE operationtype=21 AND d.invoicestatus in (7,8) AND b.invoiceid =d.invoiceid AND to_char(d.dateentered,'MMYY')='1015' AND v.vendorid=d.vendorid and v.status=0) a 

WHERE c.invoiceid=b.invoiceid and c.invoiceid=a.invoiceid(+) and a.invoiceid is null; 

回答

2

這聽起來像你只是想取代

to_char(d.dateentered,'MMYY')='1015' 

trunc(d.dateEntered, 'MM') = trunc(sysdate, 'MM') 

sysdate返回當前日期。 trunc(<<date>>, 'MM')在給定輸入的月份的第一個月返回午夜。

+0

使用'to_char'和'trunc'有什麼區別? –

+0

@JuanCarlosOropeza - 如果您的目的是比較日期,我的偏見總是比較日期表達式而不是比較字符串。特別是2位數年份的字符串。 'trunc(<>,'MM')'會比'to_char(<>,'MMYY')稍微更有效率,儘管在這一點上你可能會構建一個基於函數的索引或者無論如何將表達式改變爲「之間」。 –

+0

謝謝@JustinCave和JuanCarlosOropeza。我非常感謝您的關注和時間來幫助我。非常感謝 – thiagovargasrj

相關問題