简体   繁体   English

Oracle SQL-解析名称字符串并将其转换为名字的名字和姓氏

[英]Oracle SQL - Parsing a name string and converting it to first initial & last name

Does anyone know how to turn this string: "Smith, John R" 有谁知道如何把这个字符串变成:“史密斯,约翰R”
Into this string: "jsmith" ? 放入此字​​符串:“ jsmith”?

I need to lowercase everything with lower() 我需要使用lower()小写所有内容
Find where the comma is and track it's integer location value 查找逗号在哪里并跟踪其整数位置值
Get the first character after that comma and put it in front of the string 获取该逗号后的第一个字符并将其放在字符串前面
Then get the entire last name and stick it after the first initial. 然后获得整个姓氏,并将其粘贴在第一个名字首字母之后。

Sidenote - instr() function is not compatible with my version 旁注-instr()函数与我的版本不兼容

Thanks for any help! 谢谢你的帮助!

Start by writing your own INSTR function - call it my_instr for example. 首先编写您自己的INSTR函数-例如,将其命名为my_instr。 It will start at char 1 and loop until it finds a ','. 它将从char 1开始并循环直到找到','。

Then use as you would INSTR. 然后像使用INSTR一样使用。

The best way to do this is using Oracle Regular Expressions feature, like this: 最好的方法是使用Oracle正则表达式功能,如下所示:

SELECT LOWER(regexp_replace('Smith, John R', 
             '(.+)(, )([A-Z])(.+)', 
             '\3\1', 1, 1)) 
  FROM DUAL;

That says, 1) when you find the pattern of any set of characters, followed by ", ", followed by an uppercase character, followed by any remaining characters, take the third element (initial of first name) and append the last name. 就是说:1)当您找到任何一组字符的模式,后跟“,”,后跟一个大写字符,再跟所有剩余的字符时,取第三个元素(名字的首字母缩写)并附加姓氏。 Then make everything lowercase. 然后将所有内容都小写。

Your side note: "instr() function is not compatible with my version" doesn't make sense to me, as that function's been around for ages. 您的旁注:“ instr()函数与我的版本不兼容”对我来说没有意义,因为该函数存在已有很长时间了。 Check your version, because Regular Expressions was only added to Oracle in version 9i. 检查您的版本,因为正则表达式仅在版本9i中添加到Oracle。

Thanks for the points. 谢谢你的观点。

-- Stew - 炖

instr() is not compatible with your version of what? instr()与您的什么版本不兼容? Oracle? 甲骨文? Are you using version 4 or something? 您使用的是版本4还是什么版本?

I have a hard time believing you don't have access to a proper instr() but if that's the case, implement your own version. 我很难相信您没有访问适当的instr()的权限,但是如果是这样,请实现自己的版本。

Assuming you have that straightened out: 假设您已经弄清楚了:

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;

Also, be careful about your assumption that all names will be in that format. 另外,请注意所有名称都采用该格式的假设。 Watch out for something other than a single space after the comma, last names having data like “Parisi, Jr.”, etc. 请注意,除了逗号后的空格以外,请注意其他内容,如“ Parisi,Jr.”等数据。

There is no need to create your own function, and quite frankly, it seems a waste of time when this can be done fairly easily with sql functions that already exist. 不需要创建自己的函数,坦率地说,使用已经存在的sql函数可以很容易地做到这一点,这似乎是在浪费时间。 Care must be taken to account for sloppy data entry. 必须小心考虑草率的数据输入。

Here is another way to accomplish your stated goal: 这是达成既定目标的另一种方法:

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, Gabe HTH,加布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM