甲骨文的SQL - 解析一个名称字符串并将其转换为第一最初和最后的名字
题
有谁知道如何把这个字符串:“史密斯,约翰R”结果 这个字符串: “jsmith的”
?我需要为小写具有较低(),点击一切 寻找到逗号和跟踪它的整数位置值结果 得到第一个字符是逗号后把它放在前面的字符串结果 然后让整个姓氏和首字母后坚持下去。搜索结果 阿里纳斯 - INSTR()函数不是与我的版本兼容的搜索结果 感谢您的帮助!
解决方案
通过编写自己的INSTR函数开始 - 称之为my_instr例如。将在炭1和循环开始,直到它找到一个“”
然后,当使用你将INSTR。
其他提示
要做到这一点,最好的方法是使用Oracle正则表达式功能,如下所示:
SELECT LOWER(regexp_replace('Smith, John R',
'(.+)(, )([A-Z])(.+)',
'\3\1', 1, 1))
FROM DUAL;
这表示,1)当你发现任何的字符集,接着“”的图案,跟着是一个大写字符,后面跟任何剩余的字符,从第三元件(第一名字的初始)和追加姓。然后使一切小写。
您侧面说明:“INSTR()函数是不是与我的版本不兼容”没有意义对我来说,作为函数的已经行之有年。检查您的版本,因为正则表达式才被加入到甲骨文版本9i中。
感谢您的点。
- 炖
INSTR()不是你的什么版本兼容?甲骨文?您是否使用版本4或什么?
有没有必要创建自己的功能,很坦率地说,这似乎是浪费时间时,这可以很容易与已经存在的SQL函数来完成。必须小心以考虑马虎数据条目。
下面是另一种方式来实现自己的既定目标:
with name_list as
(select ' Parisi, Kenneth R' name from dual)
select name
-- There may be a space after the comma. This will strip an arbitrary
-- amount of whitespace from the first name, so we can easily extract
-- the first initial.
, substr(trim(substr(name, instr(name, ',') + 1)), 1, 1) AS first_init
-- a simple substring function, from the first character until the
-- last character before the comma.
, substr(trim(name), 1, instr(trim(name), ',') - 1) AS last_name
-- put together what we have done above to create the output field
, lower(substr(trim(substr(name, instr(name, ',') + 1)), 1, 1)) ||
lower(substr(trim(name), 1, instr(trim(name), ',') - 1)) AS init_plus_last
from name_list;
HTH, 加布
我很难相信你没有获得适当的INSTR(),但如果是这样的话,实现自己的版本。
假设你有理顺:
select substr( lower( 'Smith, John R' ) , instr( 'Smith, John R', ',' ) + 2 , 1 ) || -- first_initial substr( lower( 'Smith, John R' ) , 1 , instr( 'Smith, John R', ',' ) - 1 ) -- last_name from dual;
此外,要小心你的假设,即所有的名字将在该格式。当心比逗号后的一个空格以外的东西,具有类似于“帕里斯,小”等数据姓氏
不隶属于 StackOverflow