Oracle中的translate函数是做什么的

What does the translate function do in Oracle

我知道解码函数,但是不知道翻译里的东西是什么意思:

decode(translate(cookie_id,'0123456789','') = '','t',cookie_id,null) as cookie_id

decode((translate(pickup_date,'0123456789','') = '--' and length(pickup_date) = 10),'t',pickup_date,null) as pickup_date,

很简单。这个表达式:

translate(cookie_id, '0123456789', '')

returns NULL。 Oracle 将空字符串视为 NULL 甚至明确警告:

You cannot use an empty string for to_string to remove all characters in from_string from the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null.

因此,第一个示例将始终为 cookie_id 生成 NULL

很可能,作者的意图是这样的:

decode(translate(cookie_id, 'a0123456789', 'a'), '', cookie_id, null) as cookie_id

这将检查 cookie_id 是否仅包含数字,然后 return id。就个人而言,我发现正则表达式更容易理解:

(case when regexp_like(cookie_id, '^[0-9]+$') then cookie_id end)

而且这不需要做一些时髦的事情来处理空字符串。