【问题标题】:Formatting names in a single column in sql oracle在sql oracle中的单列中格式化名称
【发布时间】:2020-05-18 22:32:16
【问题描述】:

我需要帮忙。我是甲骨文的新手。我有一个简单的问题。

我的桌子:

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.

我尝试了很多方法,但我无法达到结果。我怎样才能得到我想要的输出?

【问题讨论】:

  • 查看函数INITCAP

标签: sql oracle formatting


【解决方案1】:

TRIM 将删除前导和尾随空格。然后INITCAP将每个单词的第一个字母大写,其余的小写。然后,您可以使用正则表达式将每个名字替换为其缩写,并交换名字和姓氏:

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. | |安德森 P.T. |

db小提琴here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2014-02-03
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多