我有一个数据类型为 VARCHAR2(18)
的 nse_credit_ rating 列。
它包含 189.00-250.00
形式的值。
但有些记录在字符串末尾有空值,即即使与我相关的数据长度为 13 (189.00-250.00
),但 length(nse_credit_ rating)
返回 17作为输出,即末尾有 4 个空值。
当我必须在此列上使用 to_number
时,会出现主要问题,它会返回“ORA-1722 无效数字”,因为它包含 null。
有没有办法可以获取 NULL 值出现之前的子字符串?
我什至获取了 dump(nse_credit_ rating)
的输出。
SELECT dump(nse_credit_rating),nse_symbol
FROM nse_security_master
where nse_series='EQ'
and nse_symbol like 'ARVIND'
and nse_series='EQ'
输出:
Typ=1 Len=17: 51,54,55,46,48,48,45,52,52,56,46,53,48,0,0,0,0
这里 0 是 NULL 的 ascii。
SELECT (nse_credit_rating),nse_symbol
FROM nse_security_master
where nse_series='EQ'
and nse_symbol like 'ARVIND'
and nse_series='EQ'
输出:
367.00-448.50
我尝试使用 ascii(substr(nse_credit_ rating,-1,1))
但这仅在我知道最后只有一个字符为 Null 时才有效。但可以有任意数量的字符具有 NULL。
最佳答案
使用rtrim()
从字符串末尾删除字符:
SELECT rtrim(nse_credit_rating, chr(0)) as nse_credit_rating
,nse_symbol
FROM nse_security_master
where nse_series='EQ'
and nse_symbol like 'ARVIND'
and nse_series='EQ'
Oracle SQL 文档对此进行了介绍。 Find out more 。 Oracle 还支持标准 SQL trim()
函数,但稍微冗长一些:
SELECT trim(trailing chr(0) from nse_credit_rating) as nse_credit_rating
, nse_symbol
FROM nse_security_master
where nse_series='EQ'
and nse_symbol like 'ARVIND'
and nse_series='EQ'
Can you tell me what chr(0) does here?
chr()
是将 ASCII 数字转换为字符的函数。所以 chr(0)
给我们 ASCII null。
关于sql - Oracle中如何忽略字符串末尾的空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55689590/