从varchar字段中仅选择单词的第一个字母 [英] select only first letters of words from a varchar field

查看:54
本文介绍了从varchar字段中仅选择单词的第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一次采访中被问到一个来自oracle sql的问题.这似乎是一个简单的问题,但是我不知道要回答什么.有人可以帮忙吗?

I was asked in an interview,a question from oracle sql.this seemed to be a simple question but i had no clue to answer.could anybody help?

如果某列中有诸如纽约是一个美丽的城市"之类的字符串.

if there is string like "newyork is a beautiful city" in a colum.

select column_name from table_name;

将导致

newyork is a beautiful city

将输出作为所有首字母的字符串提供所需的查询是什么. 即输出应为

what is the query required to give the output as a string with all the first letters. i.e., the output should be

niabc

推荐答案

只要您不关心维护输出的大小写,就可以很简单地完成此操作,而无需递归:

Provided you're not concerned with maintaining the case of the output this can be done quite simply without the need for recursion:

SQL> select
  2      translate(
  3            initcap('newyork is a BEAUTIFUL city')
  4               , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz'
  5               , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  6              )
  7  from dual
  8  /

TRANS
-----
NIABC

SQL>

如果句子中包含数字,标点符号等,那么我们将不得不将这些字符添加到第一个替换字符串中,这可能会变得很乏味.

If the sentence contains numerals, punctuation, etc then we would have to add those characters to the first replacement string, which could get rather tedious.

这篇关于从varchar字段中仅选择单词的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆