INITCAP 函数
将指定字符串中的每个单词的第一个字母大写。INITCAP 支持 UTF-8 多字节字符,并且每个字符最多可以有 4 个字节。
语法
INITCAP(string)
参数
- string
-
CHAR
字符串、VARCHAR
字符串或隐式计算为CHAR
或VARCHAR
类型的表达式。
返回类型
VARCHAR
使用说明
INITCAP 函数会将字符串中的每个单词的第一个字母大写,并将所有后续字母小写。因此,务必了解哪些字符(空格字符除外)充当分隔符。文字分隔符 字符是任何非字母数字字符,包括标点符号、普通符号和控制字符。所有以下字符都是文字分隔符:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
制表符、换行符、换页符和回车也是文字分隔符。
示例
以下示例使用 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 |
+------------------------------------+------------------------------------+