Oracle NVL函数不允许第二个参数作为datetime

时间:2015-06-18 13:36:23

标签: oracle datetime nvl

select nvl(trunc(null),trunc(sysdate)) from dual;

执行上述查询时,我收到以下错误

ORA-00932: inconsistent datatypes: expected NUMBER got DATE

看起来当我接受字符串或数字而不是trunc(sysdate)时,它运行正常。

2 个答案:

答案 0 :(得分:5)

来自here

  

NVL的第一个参数是确定的预期数据类型   返回列,截断函数默认为NUMBER   因为它的参数是NULL。 NVL的第二个参数需要   匹配它没有的数据类型,因为它是一个日期。

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;

答案 1 :(得分:1)

第二个参数必须与第一个参数具有相同的数据类型。 package Assignment_1; //Begin Class Definition public class AddressBook { // Member variables private String businessPhone; private String cellPhone; private String facebookId; private String firstName; private String homeAddress; private String homePhone; private String lastName; private String middleName; private String personalWebSite; private String skypeId; //Constructors public AddressBook (String firstName, String middleName, String lastName, String homeAddress, String businessPhone, String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.homeAddress = homeAddress; this.businessPhone = businessPhone; this.homePhone = homePhone; this.cellPhone = cellPhone; this.skypeId = skypeId; this.facebookId = facebookId; this.personalWebSite = personalWebSite; } public AddressBook (String firstName) { this.firstName = firstName; } public AddressBook(String firstName, String middleName) { this.firstName = firstName; this.middleName = middleName; } public AddressBook (String firstName, String middleName, String lastName) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; } // Getters and setters public String getFirstName() { return firstName; } public String getMiddleName() { return middleName; } public String getLastName() { return lastName; } public String getHomeAddress() { return homeAddress; } public String getBusinessPhone() { return businessPhone; } public String getHomePhone() { return homePhone; } public String getCellPhone() { return cellPhone; } public String getSkypeId() { return skypeId; } public String getFacebookId() { return facebookId; } public String getPersonalWebsite() { return personalWebSite; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public void setBusinessPhone(String businessPhone) { this.businessPhone = businessPhone; } public void setHomePhone(String homePhone) { this.homePhone = homePhone; } public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; } public void setSkypeId(String skypeId) { this.skypeId = skypeId; } public void setFacebookId(String facebookId) { this.facebookId = facebookId; } public void setPersonalWebSite(String personalWebSite) { this.personalWebSite = personalWebSite; } // Public methods public static void compareNames(String name1, String name2) { if(name1.equals(name2)) { System.out.println(name1); System.out.println(name2); System.out.println("The names are the same."); } else { System.out.println(name1); System.out.println(name2); System.out.println("The names appear to be different."); } } ************************************************************ package Assignment_1; public class BanffMarathonRunner extends AddressBook { // Member variables private int time; private int years; // Constructors public BanffMarathonRunner(String firstName, String lastName, int min, int yr) { super(firstName, lastName); time = min; years = yr; } // Getters and Setters public int getTime() { return time; } public void setTime(int time) { this.time = time; } public int getYears() { return years; } public void setYears(int years) { this.years = years; } } ************************************************************ package Assignment_1; import Assignment_1.BanffMarathonRunner; public class TestBanffMarathonRunner { public static void main(String[] args) { BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1); System.out.print(r1.getLastName()); } } }函数已重载。显然,当你传递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;

为了得到实际解决问题的答案,我建议编辑问题以包含实际证明问题的代码。