如何在 Oracle 中不使用 Initcap 只转换首字母大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35290182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:09:16  来源:igfitidea点击:

How to convert only first letter uppercase without using Initcap in Oracle?

sqloracle

提问by jarlh

Is there a way to convert the first letter uppercase in Oracle SQl without using the Initcap Function?

有没有办法在不使用 Initcap 函数的情况下转换 Oracle SQl 中的第一个字母大写?

I have the problem, that I must work with the DISTINCT keyword in SQL clause and the Initcap function doesn′t work.

我有一个问题,我必须在 SQL 子句中使用 DISTINCT 关键字,而 Initcap 函数不起作用。

Heres is my SQL example:

这是我的 SQL 示例:

select distinct p.nr, initcap(p.firstname), initcap(p.lastname), ill.describtion
from patient p left join illness ill
  on p.id = ill.id          
where p.deleted = 0
order by p.lastname, p.firstname;

I get this error message: ORA-01791: not a SELECTed expression

我收到此错误消息:ORA-01791: not a SELECTed expression

回答by jarlh

When SELECT DISTINCT, you can't ORDER BYcolumns that aren't selected. Use column aliases instead, as:

当 时SELECT DISTINCT,您ORDER BY不能选择未选中的列。改用列别名,如:

select distinct p.nr, initcap(p.firstname) fname, initcap(p.lastname) lname, ill.describtion
from patient p left join illness ill
  on p.id = ill.id          
where p.deleted = 0
order by lname, fname

回答by davegreen100

this would do it, but i think you need to post your query as there may be a better solution

这样做可以,但我认为您需要发布您的查询,因为可能有更好的解决方案

select upper(substr(<column>,1,1)) || substr(<column>,2,9999) from dual

回答by RAG

To change stringto String, you can use this:

要更改stringString,您可以使用:

SELECT
regexp_replace ('string', '[a-z]', upper (substr ('string', 1, 1)), 1, 1, 'i')
FROM dual;

This assumes that the first letter is the one you want to convert. It your input text starts with a number, such as 2 stringsthen it won't change it to 2 Strings.

这假设第一个字母是您要转换的字母。如果您的输入文本以数字开头,2 strings则不会将其更改为2 Strings.

回答by Daniela

You can also use the column number instead of the name or alias:

您还可以使用列号代替名称或别名:

select distinct p.nr, initcap(p.firstname), initcap(p.lastname), ill.describtion
from patient p left join illness ill
  on p.id = ill.id          
where p.deleted = 0
order by 3, 2;