[英]Formatting names in a single column in sql oracle
我需要帮忙。 我是 oracle 的新手。 我有一个简单的问题。
我的桌子:
create table employee (name varchar2(30));
insert into employee values ('kevin durant');
insert into employee values ('michael JoRdaN');
insert into employee values (' dWyaNe WAdE');
insert into employee values ('james Harden');
insert into employee values ('pAuL ThomaS AnDersoN');
我想要的格式如下:
Durant K.
Jordan M.
Wade D.
Harden J.
Anderson P.T.
我尝试了很多方法,但我无法达到结果。 如何获得我想要的 output?
TRIM
将删除前导和尾随空格。 然后INITCAP
将每个单词的首字母大写,并将rest变成小写。 然后,您可以使用正则表达式将每个名字替换为其缩写,并交换名字和姓氏:
SELECT REGEXP_REPLACE(
REGEXP_REPLACE(
INITCAP(TRIM(name)),
'(\S)\S*\s+', -- Match a forename with a trailing space
'\1.' -- And replace it with the abbreviation
),
'(.*\.)(.+)', -- Match the substrings before and after the last
-- full stop
'\2 \1' -- And swap them.
) AS Name
FROM employee
其中,对于您的测试数据,输出:
| 姓名 | |:------------ | | 杜兰特K。 | 乔丹·M。 | 韦德 D。 | 哈登 J. | | 安德森 PT |
db<> 在这里摆弄
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.