2015-06-18 84 views
3
select nvl(trunc(null),trunc(sysdate)) from dual; 

在執行上面的查詢我收到以下錯誤甲骨文NVL函數不是允許第二個參數日期時間

ORA-00932: inconsistent datatypes: expected NUMBER got DATE 

看,當我需要字符串或數字,而不是trunc(sysdate)它運行正常等。

+1

'NVL()'需要有與參數類似的數據類型! –

+2

DId你只是嘗試'TRUNC(NVL(yourcolumn,SYSDATE))'..這將是乾淨的解決方案。 –

+0

@MaheswaranRavisankar是多數民衆贊成在太多,thanx –

回答

4

here

The first parameter to NVL is determining the expected datatype of the returned column, with the trunc function defaulting that to NUMBER because it's parameter is NULL. The second parameter to NVL needs to match that datatype which it doesn't because it is a date.

SQL> select nvl(trunc(sysdate), sysdate) as mydate from dual; 

MYDATE 
------------------- 
26/05/2006 00:00:00 

SQL> select nvl(trunc(null), sysdate) as mydate from dual; 
select nvl(trunc(null), sysdate) as mydate from dual 
         * 
ERROR at line 1: 
ORA-00932: inconsistent datatypes: expected NUMBER got DATE 

,或者你可以這樣做:

SELECT NVL(TO_DATE(TRUNC(NULL)),SYSDATE) FROM dual; 
+0

在我真正的查詢我有一個日期時間參數,返回null,'從雙選擇nvl(trunc(pd_eftv_to),trunc(sysdate));' –

+0

@RashminJaviya: - 你可以嘗試像這個:'select nvl(TO_DATE(trunc(pd_eftv_to)),TO_DATE(trunc(sysdate)))from dual;' –

1

第二個參數必須是相同的數據類型作爲第一個參數。 trunc函數被重載。顯然,當你通過一個null時,它將該值解釋爲一個數字並返回一個數字。以下任何作品:

SELECT NVL (NULL, TRUNC (SYSDATE)) FROM DUAL; 

SELECT NVL (TRUNC (TO_DATE (NULL)), TRUNC (SYSDATE)) FROM DUAL; 

SELECT NVL (TRUNC (CAST (NULL AS DATE)), TRUNC (SYSDATE)) FROM DUAL; 

追問:

如果正在使用的變量定義正確的日期,那麼你應該不會有問題。作爲一個例子,沒有錯誤以下運行:

DECLARE 
    FUNCTION f 
     RETURN DATE IS 
    BEGIN 
     RETURN NULL; 
    END f; 
BEGIN 
    DBMS_OUTPUT.put_line (NVL (TRUNC (f), TRUNC (SYSDATE))); 
END; 

爲了得到實際解決您的問題的答案,我會建議編輯問題,包括實際演示該問題的代碼。

+0

在我真正的查詢中,我有一個返回null的datetime參數,'select nvl(trunc(pd_eftv_to),trunc (sysdate))來自dual;' –