oracle ORA-00932: 不一致的数据类型:预期的 CHAR 得到 NUMBER

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27802128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:40:51  来源:igfitidea点击:

ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

sqloracleoracle-sqldeveloper

提问by Matt

Logic

逻辑

The logic is if an order is cancelled then return 0 otherwise return the owed value - the paid value

逻辑是如果订单被取消则返回 0 否则返回欠值 - 支付值

Small query

小查询

CASE WHEN d.cancelled = 'TRUE' 
     THEN '0' 
     ELSE (to_char(b.owed)) - (to_char(d.paid)) 
     END AS balance,

Getting the error

得到错误

ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent datatypes: expected %s got %s" *Cause:
*Action: Error at Line: 25 Column: 58

ORA-00932:不一致的数据类型:预期的 CHAR 得到 NUMBER 00932。00000 - “不一致的数据类型:预期的 %s 得到 %s” *原因:
*操作:行错误:25 列:58

回答by HaveNoDisplayName

Try this, either your case should return number or varchar, right now your case return '0' as varchar and else as number. Either both should return a varchar or both should return a number.

试试这个,你的情况应该返回数字或varchar,现在你的情况将'0'作为varchar返回,否则作为数字返回。两者都应该返回一个 varchar 或者都应该返回一个数字。

When Both return varchar

当两者都返回 varchar 时

CASE WHEN d.cancelled = 'TRUE' 
     THEN '0' 
     ELSE to_char((to_char(b.owed)) - (to_char(d.paid)))
     END AS balance,

OR

或者

When Both return number

当两者都返回数字时

CASE WHEN d.cancelled = 'TRUE' 
         THEN 0 
         ELSE (to_char(b.owed)) - (to_char(d.paid))
         END AS balance,

OR

或者

When Both return number

当两者都返回数字时

CASE WHEN d.cancelled = 'TRUE' 
         THEN 0 
         ELSE (b.owed - d.paid)
         END AS balance,

回答by Jens

This (to_char(b.owed)) - (to_char(d.paid))will be a number and 0is a string. You should remove the ticks in thenclause or ad a to_char in else clause: to_char((to_char(b.owed)) - (to_char(d.paid)))or simply (b.owed - d.paid)

(to_char(b.owed)) - (to_char(d.paid))将是一个数字并且0是一个字符串。您应该删除then子句中的刻度或在 else 子句中添加 to_char:to_char((to_char(b.owed)) - (to_char(d.paid)))或者干脆(b.owed - d.paid)