In this section, we cover how to set the case of a string.
There are four common case settings:
We wish to set the case of a string column to lower case.
In this example, we wish to set the case of all the individual string values in the string
column col_1
to lower case.
SELECT col_1, LOWER(col_1) col_2
FROM table_1;
Here is how this works:
LOWER()
function to set the case of all letters in each string element in the
column col_1
to lower case.LOWER()
function takes one argument which is the string column (or
individual string literal) whose case is to be set to lower case.table_1
but
with an additional column col_2
holding an all lower case version of the column col_1
.We wish to set the case of a string column (or literal) to upper case.
In this example, we wish to set the case of all the individual string values in the string
column col_1
to upper case.
SELECT col_1, UPPER(col_1) col_2
FROM table_1;
Here is how this works:
UPPER()
function to set the case of all letters in each string element in the
column col_1
to upper case.LOWER()
function takes one argument which is the string column (or
individual string literal) whose case is to be set to upper case.table_1
but
with an additional column col_2
holding an all upper case version of the column col_1
.We wish to set the case of a string column (or literal) to title capitalization. Title capitalization is where the first character of each word is upper case and the rest is lower case e.g. "String Formatting".
In this example, we wish to set the case of all the individual string values in the string
column col_1
to title case.
SELECT col_1, INITCAP(col_1) col_2
FROM table_1
Here is how this works:
INITCAP()
function to set the case of all letters in each string element in the
column col_1
to title case.INITCAP()
function takes one argument which is the string column (or
individual string literal) whose case is to be set to title case.table_1
but
with an additional column col_2
holding an all title case version of the column col_1
.We wish to set the case of a string column (or literal) to sentence capitalization. Sentence
capitalization is where the first character of the first word is upper case and the rest is lower
case e.g. "String formatting is cool.".
In this example, we wish to set the case of all the individual string values in the string
column col_1
to sentence case.
SELECT col_1,
CONCAT(UPPER(SUBSTR(col_1, 1, 1)),
LOWER(SUBSTR(col_1, 2))) AS col_2
FROM table_1;
Here is how this works:
col_1
to sentence case.UPPER()
, LOWER()
, SUBSTR()
,
and CONCAT()
.CONCAT()
function is used to concatenate, or join, multiple strings together. In this case,
it is being used to create a new string by combining the following three strings:UPPER(SUBSTR(col_1, 1, 1))
: SUBSTR(col_1, 1, 1)
is the first character of col_1
,
converted to uppercase using the UPPER
function.LOWER(SUBSTR(col_1, 2))
: SUBSTR(col_1, 2)
is the rest of col_1
, starting from the second
character and going to the end, converted to lowercase using the LOWER()
function.table_1
but
with an additional column col_2
holding an all sentence case version of the column col_1
.Alternative: Using a user defined function
CREATE OR REPLACE FUNCTION dataset.SENTENCE(val STRING)
RETURNS STRING
AS
(
CONCAT(UPPER(SUBSTR(val, 1, 1)),
LOWER(SUBSTR(val, 2)))
);
WITH table_1 AS (SELECT *
FROM UNNEST(['format a string',
'set to lower case',
'try Upper case']) AS col_1)
SELECT col_1, dataset.SENTENCE(col_1) col_2
FROM table_1;
Here is how this works:
UDF
to convert a column to sentence case.SENTENCE
and takes a string as input and returns a modified version of
the string. The function is defined using the CREATE OR REPLACE FUNCTION
statement.dataset.SENTENCE(val STRING)
: This is the function signature, which specifies the name of
the function (SENTENCE
) and the input parameter (val
) of type STRING
. The dataset.
prefix indicates that the function belongs to a dataset called dataset
.RETURNS STRING
: This specifies the return type of the function, which is a string in this
case.(CONCAT(UPPER(SUBSTR(val, 1, 1)), LOWER(SUBSTR(val, 2))))
: This is the body of the function,
which defines the logic for transforming the input string. It works similarly to the solution
provided above.SELECT
statement then selects the col_1
column from table_1
and applies
the SENTENCE
function to each value in the column, creating a new column called col_2
that
contains the modified strings.