开发者

Formatting String out from Oracle Select Query

开发者 https://www.devze.com 2023-02-14 03:55 出处:网络
Lets say when I run the following query, SELECT NAME 开发者_如何学运维 FROM EMP; It return the following 2 rows.

Lets say when I run the following query,

SELECT NAME 开发者_如何学运维
  FROM EMP;

It return the following 2 rows.

NAME
------------
Jan Jones
Arne Barnie

But I wanted it in the following format

J. Jones
A. Barnie

How I can get that?


It should be possible with regexp_replace:

with names as (
  select 'Jan Jones'   name from dual union all
  select 'Arne Barnie' name from dual
)
select
  regexp_replace(name, '(.)[[:alpha:]]* *([[:alpha:]]*)', '\1. \2') name
from
  names;

Alternatively, if you prefer the \X notation over the [[:...:]] notation, you can use

regexp_replace(name, '(.)\w* *(\w*)', '\1. \2') 


There are no short cuts like DECODE to do this I think this is your best bet.

0

精彩评论

暂无评论...
验证码 换一张
取 消