|
| 1 | +--- |
| 2 | +title: CITEXT |
| 3 | +summary: The CITEXT data type stores case-insensitive text values. |
| 4 | +toc: true |
| 5 | +docs_area: reference.sql |
| 6 | +--- |
| 7 | + |
| 8 | +The `CITEXT` [data type]({% link {{ page.version.version }}/data-types.md %}) stores case-insensitive strings. |
| 9 | + |
| 10 | +All `CITEXT` values are folded to lowercase before comparison. This is handled internally with the [`lower()`]({% link {{ page.version.version }}/functions-and-operators.md %}#string-and-byte-functions) function. |
| 11 | + |
| 12 | +For example, the following comparison evaluates to `true`: |
| 13 | + |
| 14 | +{% include_cached copy-clipboard.html %} |
| 15 | +~~~ sql |
| 16 | +SELECT 'Roach'::CITEXT = 'roach'::CITEXT; |
| 17 | +~~~ |
| 18 | + |
| 19 | +~~~ |
| 20 | + ?column? |
| 21 | +------------ |
| 22 | + t |
| 23 | +~~~ |
| 24 | + |
| 25 | +With `CITEXT`, equality operators (`=`, `!=`, `<>`), ordering operators (`<`, `>`, etc.), and [`STRING` functions]({% link {{ page.version.version }}/functions-and-operators.md %}#string-and-byte-functions), treat values as case-insensitive by default. Refer to the [example](#example). |
| 26 | + |
| 27 | +Aside from comparisons, `CITEXT` behaves like [`STRING`]({% link {{ page.version.version }}/string.md %}). |
| 28 | + |
| 29 | +## Syntax |
| 30 | + |
| 31 | +To declare a `CITEXT` column, use the type name directly in your `CREATE TABLE` statement: |
| 32 | + |
| 33 | +{% include_cached copy-clipboard.html %} |
| 34 | +~~~ sql |
| 35 | +CREATE TABLE logins ( |
| 36 | + name CITEXT PRIMARY KEY, |
| 37 | + email TEXT NOT NULL |
| 38 | +); |
| 39 | +~~~ |
| 40 | + |
| 41 | +## Size |
| 42 | + |
| 43 | +As with `STRING`, `CITEXT` values should be kept below 64 KB for best performance. Because `CITEXT` values are folded to lowercase on every comparison, `CITEXT` columns and indexes consume marginally more CPU and memory than their `STRING` equivalents, especially on write-heavy workloads. |
| 44 | + |
| 45 | +## Collations |
| 46 | + |
| 47 | +`CITEXT` compares values as a `STRING` column with the `und-u-ks-level2` [collation]({% link {{ page.version.version }}/collate.md %}), meaning it is case-insensitive but accent-sensitive. If you need accent-insensitive behavior, consider using `STRING` with a nondeterministic collation instead. |
| 48 | + |
| 49 | +## Example |
| 50 | + |
| 51 | +Create and populate a table: |
| 52 | + |
| 53 | +{% include_cached copy-clipboard.html %} |
| 54 | +~~~ sql |
| 55 | +CREATE TABLE logins ( |
| 56 | + username CITEXT, |
| 57 | + email STRING |
| 58 | +); |
| 59 | +~~~ |
| 60 | + |
| 61 | +{% include_cached copy-clipboard.html %} |
| 62 | +~~~ sql |
| 63 | +INSERT INTO logins VALUES |
| 64 | +('roach', 'roach@example.com'), |
| 65 | +('juno', 'juno@example.com'); |
| 66 | +~~~ |
| 67 | + |
| 68 | +Because `CITEXT` comparisons are case-insensitive, an equality predicate matches regardless of letter case: |
| 69 | + |
| 70 | +{% include_cached copy-clipboard.html %} |
| 71 | +~~~ sql |
| 72 | +SELECT * FROM logins WHERE username = 'Roach'; |
| 73 | +~~~ |
| 74 | + |
| 75 | +~~~ |
| 76 | + name | email |
| 77 | +--------+-------------------- |
| 78 | + roach | roach@example.com |
| 79 | +(1 row) |
| 80 | +~~~ |
| 81 | + |
| 82 | +An ordering comparison is also case-insensitive with `CITEXT`. In the following eaxmple, `'Xavi'` is folded to lowercase before the comparison: |
| 83 | + |
| 84 | +{% include_cached copy-clipboard.html %} |
| 85 | +~~~ sql |
| 86 | +SELECT username FROM logins WHERE username < 'Xavi'; |
| 87 | +~~~ |
| 88 | + |
| 89 | +~~~ |
| 90 | + username |
| 91 | +------------ |
| 92 | + roach |
| 93 | + juno |
| 94 | +(2 rows) |
| 95 | +~~~ |
| 96 | + |
| 97 | +For case-sensitive comparisons on `CITEXT` values, cast to `STRING` explicitly. In the default Unicode ordering, the uppercase value is considered less than the lowercase values in the table: |
| 98 | + |
| 99 | +{% include_cached copy-clipboard.html %} |
| 100 | +~~~ sql |
| 101 | +SELECT username FROM logins WHERE username::STRING < 'Xavi'; |
| 102 | +~~~ |
| 103 | + |
| 104 | +~~~ |
| 105 | + username | email |
| 106 | +-----------+-------- |
| 107 | +(0 rows) |
| 108 | +~~~ |
| 109 | + |
| 110 | +## Supported casting and conversion |
| 111 | + |
| 112 | +`CITEXT` values can be [cast]({% link {{ page.version.version }}/data-types.md %}#data-type-conversions-and-casts) to the following data types: |
| 113 | + |
| 114 | +Type | Details |
| 115 | +-----|-------- |
| 116 | +`STRING` | Preserves case information when casting to `STRING`. |
| 117 | + |
| 118 | +## See also |
| 119 | + |
| 120 | +- [`STRING`]({% link {{ page.version.version }}/string.md %}) |
| 121 | +- [Data Types]({% link {{ page.version.version }}/data-types.md %}) |
| 122 | +- [`COLLATE`]({% link {{ page.version.version }}/collate.md %}) |
0 commit comments