DEV Community

Michael
Michael

Posted on

initcap in GBase 8a: Capitalizing the First Letter of Every Word

initcap converts the first letter of each word in a string to uppercase and the rest to lowercase. This post walks through its behavior in the China-domestically developed GBase 8a MPP database from GBASE, including how it treats full-width characters and punctuation as word separators.

Syntax

initcap(expr)
Enter fullscreen mode Exit fullscreen mode

expr can be a string, a column, the result of a function, a constant, and so on. A letter here means a Unicode letter: Latin letters, Arabic digits, Chinese pinyin, Chinese characters, Japanese hiragana and katakana, Western European letters and others. The word separator cannot be configured — by default a space, a tab or a newline splits words.

Use

Latin letters

The first character becomes uppercase, the rest lowercase.

gbase> select initcap('hello wORLD');
+------------------------+
| initcap('hello wORLD') |
+------------------------+
| Hello World            |
+------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase>
Enter fullscreen mode Exit fullscreen mode

Full-width Latin letters

Full-width pinyin letters behave the same way:

gbase> select initcap('abc');
+----------------------+
| initcap('abc')    |
+----------------------+
| Abc               |
+----------------------+
1 row in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

Separators

Beyond the ASCII space, other separators work as follows.

Newline as a word separator

gbase> select initcap('hello\nwORLD');
+-------------------------+
| initcap('hello\nwORLD') |
+-------------------------+
| Hello
World             |
+-------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

TAB as a separator

gbase> select initcap('hello\twORLD');
+-------------------------+
| initcap('hello\twORLD') |
+-------------------------+
| Hello World             |
+-------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select initcap('hello\t\twORLD');
+---------------------------+
| initcap('hello\t\twORLD') |
+---------------------------+
| Hello         World              |
+---------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

Full-width space

gbase> select initcap('hello wORLD');
+--------------------------+
| initcap('hello wORLD')  |
+--------------------------+
| Hello World             |
+--------------------------+
1 row in set (Elapsed: 00:00:00.00)

Enter fullscreen mode Exit fullscreen mode

Punctuation, including full-width

Including hyphens, underscores and similar marks.

gbase> select initcap('hello,wORLD');
+--------------------------+
| initcap('hello,wORLD')  |
+--------------------------+
| Hello,World             |
+--------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select initcap('hello,wORLD');
+------------------------+
| initcap('hello,wORLD') |
+------------------------+
| Hello,World            |
+------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select initcap('hello-wORLD');
+------------------------+
| initcap('hello-wORLD') |
+------------------------+
| Hello-World            |
+------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select initcap('hello_wORLD');
+------------------------+
| initcap('hello_wORLD') |
+------------------------+
| Hello_World            |
+------------------------+
1 row in set (Elapsed: 00:00:00.00)

Enter fullscreen mode Exit fullscreen mode

Punctuation and whitespace split words, so hello,wORLD becomes Hello,World while hello-wORLD becomes Hello-World. The full-width characters in the samples above are kept verbatim because they are the very characters being tested.

Going forward, remember that initcap in gbase SQL works on Unicode letters with a fixed set of separators: it is handy for normalizing display strings, but a gbase database will treat every space, tab, newline and punctuation mark as a word boundary, so do not expect it to preserve deliberate mixed case inside a token.

Top comments (0)