为什么oracle sql中的第一个字符会失败?
select DECODE( TRANSLATE('1','123',' '), NULL, 'number','contains char') from dual
这是有效的,因为1是第二个数字
select DECODE( TRANSLATE('1','4123',' '), NULL, 'number','contains char') from dual
但这失败了因为4是第一个数字
select DECODE( TRANSLATE('4','423',' '), NULL, 'number','contains char') from dual
答案 0 :(得分:2)
首先让我们来看看翻译函数定义:
this.setStuff = function(s){
Object.assign(this.stuff, s);
}
即。 TRANSLATE(some_string,'123','abc'):1将被替换为a,2乘b,3乘c(我将使用箭头 - >而不是“替换为”进一步)
现在让我们来看看我们的例子:
TRANSLATE(expr, from_string, to_string): TRANSLATE returns expr with all
occurrences of each character in from_string replaced by its corresponding
character in to_string. Characters in expr that are not in from_string are not replaced.
If expr is a character string, then you must enclose it in single quotation marks.
The argument from_string can contain more characters than to_string. In this case,
the extra characters at the end of from_string have no corresponding characters
in to_string. If these extra characters appear in char, then they are removed
from the return value.
上述函数的结果是由空格组成的字符串 - “”
TRANSLATE('1','123',' '): 1 -> " ", 2->nothing, 3->nothing.
(nothing means removed from the return value, see definition)
上述函数的结果为空字符串“”。 Oracle数据库将空字符串解释为null,如果此函数具有null参数,则返回null。
TRANSLATE('1','4123',' '): 4 -> " ", 1->nothing, 2->nothing, 3->nothing
上述函数的结果是第一个例子中的空白字符串。
这就是为什么你在第一个和第三个查询中得到“包含char”,在第二个查询中得到数字