INITCAP 函數 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

INITCAP 函數

將指定字串中每個單字的第一個字母變成大寫。INITCAP 支援 UTF-8 多位元組字元,每個字元最多 4 個位元組。

語法

INITCAP(string)

引數

string

CHAR 字串、VARCHAR 字串或隱含評估為 CHARVARCHAR 類型的運算式。

傳回類型

VARCHAR

使用須知

INITCAP 函數將字串中每個單字的第一個字母變成大寫,而後續任何字母都變成 (或保持) 小寫。因此,必須了解哪些字元 (除了空白字元) 做為單字分隔符號。單字分隔符號字元是任何非英數字元,包括標點符號、記號及控制字元。以下所有字元都是單字分隔符號:

! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

Tab、新行字元、換頁、換行及歸位也是單字分隔符號。

範例

下列範例會使用 TICKIT 範例資料庫中 CATEGORY 和 USERS 表格中的資料。如需詳細資訊,請參閱 範本資料庫

若要將 CATDESC 欄中每個單字的首字母變成大寫,請使用下列範例。

SELECT catid, catdesc, INITCAP(catdesc) FROM category ORDER BY 1, 2, 3; +-------+--------------------------------------------+--------------------------------------------+ | catid | catdesc | initcap | +-------+--------------------------------------------+--------------------------------------------+ | 1 | Major League Baseball | Major League Baseball | | 2 | National Hockey League | National Hockey League | | 3 | National Football League | National Football League | | 4 | National Basketball Association | National Basketball Association | | 5 | Major League Soccer | Major League Soccer | | 6 | Musical theatre | Musical Theatre | | 7 | All non-musical theatre | All Non-Musical Theatre | | 8 | All opera and light opera | All Opera And Light Opera | | 9 | All rock and pop music concerts | All Rock And Pop Music Concerts | | 10 | All jazz singers and bands | All Jazz Singers And Bands | | 11 | All symphony, concerto, and choir concerts | All Symphony, Concerto, And Choir Concerts | +-------+--------------------------------------------+--------------------------------------------+

若要顯示 INITCAP 函數不保留非單字開頭的大寫字元,請使用下列範例。例如,字串 MLB 會變成 Mlb

SELECT INITCAP(catname) FROM category ORDER BY catname; +-----------+ | initcap | +-----------+ | Classical | | Jazz | | Mlb | | Mls | | Musicals | | Nba | | Nfl | | Nhl | | Opera | | Plays | | Pop | +-----------+

若要將空格以外的非英數字元顯示為文字分隔符號,請使用下列範例。每個字串中的幾個字母將被大寫。

SELECT email, INITCAP(email) FROM users ORDER BY userid DESC LIMIT 5; +------------------------------------+------------------------------------+ | email | initcap | +------------------------------------+------------------------------------+ | urna.Ut@egetdictumplacerat.edu | Urna.Ut@Egetdictumplacerat.Edu | | nibh.enim@egestas.ca | Nibh.Enim@Egestas.Ca | | in@Donecat.ca | In@Donecat.Ca | | sodales@blanditviverraDonec.ca | Sodales@Blanditviverradonec.Ca | | sociis.natoque.penatibus@vitae.org | Sociis.Natoque.Penatibus@Vitae.Org | +------------------------------------+------------------------------------+