postgresql中的initcap

发布于 2025-02-11 08:09:06 字数 251 浏览 2 评论 0 原文

I need to exchage a part of email before @, i.e. 'MARIE.SUE' from '[email protected]'. Then i need to make first symbol UPPER - 'Marie.sue'. Initcap makes this: 'Marie.Sue'. How can i achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2025-02-18 08:09:06

使用 split_part initcap

SELECT INITCAP(split_part('[email protected] ','@',1))||'@' 
|| split_part(' 


 | ?柱子? |
| :-------------------------- |
| 

 选择initcap(split_part(' 


 | ?柱子? |
| :-------------------------- |
|  Email 


db<>>

With split_part and INITCAP

SELECT INITCAP(split_part('[email protected]','@',1)) ||'@' 
|| split_part('[email protected]','@',2)
| ?column?              |
| :-------------------- |
| [email protected] |
SELECT INITCAP(split_part('[email protected],uk','@',1)) ||'@' 
|| split_part('[email protected]','@',2)
| ?column?               |
| :--------------------- |
| [email protected] |

db<>fiddle here

青衫负雪 2025-02-18 08:09:06

使用PostgreSQL 9.6(或更高)尝试使用 REGEXP_SPLIT_TO_ARRAY(),因为支持了积极的lookBehind:

WITH S AS (SELECT REGEXP_SPLIT_TO_ARRAY('[email protected]', '(?<=^.|@)') AS ARR) SELECT CONCAT(UPPER(ARR[1]),LOWER(ARR[2]),ARR[3]) FROM S

请参阅在线 demo


这将是:

  • 大写第一个角色;
  • 在“@'之前,其他任何内容;
  • 在“@”之后保持所有内容。

模式(?字面的“@”。


注意:,如果您不希望从'@' - 开始的子字符串,请尝试:

WITH S AS (SELECT REGEXP_SPLIT_TO_ARRAY('[email protected]', '(?<=^.)|@.*') AS ARR) SELECT CONCAT(UPPER(ARR[1]),LOWER(ARR[2])) FROM S

或,不 REGEX()

WITH S AS (SELECT LOWER(SPLIT_PART('[email protected]', '@', 1)) AS X) SELECT UPPER(LEFT(X,1))||SUBSTRING(X,2) FROM S

请参阅在线 demo

With PostgreSQL 9.6 (or above) try to utilize REGEXP_SPLIT_TO_ARRAY() since a positive lookbehind is supported:

WITH S AS (SELECT REGEXP_SPLIT_TO_ARRAY('[email protected]', '(?<=^.|@)') AS ARR) SELECT CONCAT(UPPER(ARR[1]),LOWER(ARR[2]),ARR[3]) FROM S

See an online demo.


This would:

  • Uppercase the first character;
  • Lowercase anything else before '@';
  • Keep everything intact after '@'.

The pattern (?<=^.|@) means to match any position preceded by the start of the string and any character or by an literal '@'.


Note: In case you don't want the substring from '@'-onwards, try:

WITH S AS (SELECT REGEXP_SPLIT_TO_ARRAY('[email protected]', '(?<=^.)|@.*') AS ARR) SELECT CONCAT(UPPER(ARR[1]),LOWER(ARR[2])) FROM S

Or, without REGEX():

WITH S AS (SELECT LOWER(SPLIT_PART('[email protected]', '@', 1)) AS X) SELECT UPPER(LEFT(X,1))||SUBSTRING(X,2) FROM S

See an online demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文