Data Manipulation   «Prev  Next»

Lesson 5 Character functions: UPPER, INITCAP, RTRIM, and SOUNDEX
Objective Apply UPPER, INITCAP, RTRIM, and SOUNDEX in Oracle AI Database 26ai, and recognize their linguistic, datatype, and performance considerations.

Oracle UPPER, INITCAP, RTRIM, and SOUNDEX

Oracle AI Database 26ai includes a broad collection of single-row character functions. A single-row function evaluates each input row and returns one result for that row. This lesson examines four established functions that remain useful for searching, displaying, cleaning, and comparing character data: UPPER, INITCAP, RTRIM, and SOUNDEX.

These functions transform the value produced by an expression; they do not change the stored value unless you use the result in an INSERT or UPDATE statement. That distinction lets you format or compare text in a query without rewriting the source data.

Character-function overview

Function Purpose Typical use
UPPER Converts letters to uppercase using binary case-mapping rules. Normalizing both sides of a case-insensitive comparison.
INITCAP Returns text with the first character of each word in uppercase and the remaining characters in lowercase. Formatting ordinary text for display.
RTRIM Removes trailing characters that belong to a specified set; the default set is a single blank. Cleaning spaces or delimiters from the right end of a value.
SOUNDEX Returns an English phonetic code. Finding candidate English names that may sound alike.

1. Converting text with UPPER

The UPPER function converts lowercase letters in a character expression to uppercase. A common use is to normalize both the column value and the search value before comparing them. The following query finds log entries containing the word TANK regardless of how the word was capitalized in the stored text:

SELECT log_text
FROM   pet_care_log
WHERE  UPPER(log_text) LIKE '%TANK%';

In application code, use a bind variable instead of joining untrusted input into the SQL text. Applying UPPER to both expressions produces consistent binary case conversion:

SELECT log_text
FROM   pet_care_log
WHERE  UPPER(log_text) LIKE '%' || UPPER(:search_term) || '%';

This pattern is convenient, but it can affect performance. Applying a function to an indexed column may prevent Oracle from using a normal index on that column. A leading percent sign also prevents a conventional B-tree index from locating a fixed starting value. For frequent searches, evaluate an appropriate function-based index, a suitable collation, or Oracle Text. Choose an indexing strategy only after examining the actual workload and execution plans.

UPPER uses binary case mapping. When an application requires language-sensitive conversion, use NLS_UPPER and specify an appropriate linguistic sort. For example:

SELECT NLS_UPPER('große', 'NLS_SORT = XGerman') AS linguistic_uppercase
FROM   dual;

The result is:

LINGUISTIC_UPPERCASE
--------------------
GROSSE

Oracle 26ai allows UPPER to accept character values including CLOB and NCLOB. Always check the documented datatype rules when applying related linguistic functions, because their direct large-object support is not identical.

2. Formatting words with INITCAP

INITCAP changes the first character of each word to uppercase and the remaining characters to lowercase. Oracle defines a word as a sequence of alphanumeric characters separated by a space or another non-alphanumeric character. The function is useful for presentation text, such as a greeting generated from a username:

SELECT 'Dear ' || INITCAP(created_by_user) ||
       ', you''re great!' AS affirmation
FROM   pet_care_log;

A possible result is:

AFFIRMATION
-------------------------
Dear Henry, you're great!
Dear Mark, you're great!
Dear Janet, you're great!

Two consecutive apostrophes inside a character literal represent one apostrophe in the returned value. The expression therefore returns you're, not a prematurely terminated string literal.

Do not treat INITCAP as a complete name-normalization system. Personal and organizational names can contain intentional capitalization that the function cannot infer. Values such as McDonald, de la Cruz, and brand names may require verified source data or application-specific rules. Use INITCAP when its deterministic word rules match the display requirement.

Like UPPER, INITCAP uses binary case mapping. NLS_INITCAP performs linguistic case conversion according to the specified or session-derived NLS_SORT value. The Dutch linguistic sort, for example, handles the initial letters in ijsland as a unit:

SELECT NLS_INITCAP('ijsland', 'NLS_SORT = XDutch') AS linguistic_title
FROM   dual;

The result is IJsland. Select linguistic behavior deliberately rather than assuming that one capitalization rule fits every language.

INITCAP directly accepts CHAR, VARCHAR2, NCHAR, and NVARCHAR2. Oracle can accept a CLOB through implicit conversion, but that is not the same as direct CLOB support. Consider the size and conversion behavior before applying it to large text.

3. Removing trailing characters with RTRIM

RTRIM removes characters from the right end of a value. When the second argument is omitted, Oracle removes trailing blanks. The following query returns both the stored state name and a display value without trailing blanks:

SELECT state_name,
       RTRIM(state_name) AS display_state_name
FROM   state_lookup;

This can be useful when a fixed-length CHAR value is displayed or concatenated. It is not necessary to add RTRIM mechanically to every equality predicate: Oracle applies blank-padded comparison semantics in some character comparisons. Understand the column datatypes and the comparison being performed before changing a predicate.

The optional second argument is a set of individual characters, not a suffix string. Oracle repeatedly removes any trailing character found in that set until it reaches a character outside the set:

SELECT RTRIM('REPORT-2026.--', '.-') AS cleaned_label
FROM   dual;

The result is:

CLEANED_LABEL
-------------
REPORT-2026

Both the period and hyphen are eligible for removal because both appear in the trim set. If you must remove one exact suffix, first confirm that the suffix exists and then use an expression designed for that requirement rather than treating the second RTRIM argument as a literal substring.

RTRIM supports character large objects directly in Oracle 26ai. As with UPPER, using it on an indexed column in a predicate can change index access. A function-based index may help a stable, frequently used expression, but it should reflect real query patterns.

4. Finding English sound-alike candidates with SOUNDEX

SOUNDEX converts a character expression to a short phonetic code based on English pronunciation rules. Two differently spelled names can produce the same code, making the function useful for generating candidates when a name may have been heard but not spelled correctly.

SELECT last_name,
       first_name
FROM   employees
WHERE  SOUNDEX(last_name) = SOUNDEX('Smythe')
ORDER  BY last_name, first_name;

A match is only a candidate match. Different names can share a code, and similar names can receive different codes. The function is designed for English and is not a general multilingual similarity algorithm. Applications should display other identifying information and let a person or a carefully designed matching process confirm the result.

Oracle returns the first four bytes of the phonetic representation and pads a shorter result with zeros. SOUNDEX directly accepts CHAR, VARCHAR2, NCHAR, and NVARCHAR2; a CLOB can be accepted through implicit conversion. If this comparison is common, test whether a function-based index on SOUNDEX(last_name) is appropriate.

Oracle AI Database 26ai also provides data-quality operators such as PHONIC_ENCODE and FUZZY_MATCH. They support more specialized phonetic encodings and similarity techniques, but they do not change the behavior of SOUNDEX. Choose a matching method according to the language, error patterns, false-match tolerance, and scale of the application.

5. Nulls, composed expressions, and indexing

These four functions return NULL when their input expression is NULL. Oracle also treats a zero-length character string as null in SQL. Consequently, a predicate such as UPPER(column_name) = 'VALUE' does not select rows where column_name is null. If the application must treat a missing value as a particular label, make that decision explicitly with COALESCE or NVL:

SELECT COALESCE(INITCAP(preferred_name), 'Customer') AS display_name
FROM   customer;

Do not substitute a label merely to avoid nulls. A missing name and the literal value Customer have different meanings, so presentation rules should not silently alter filtering, grouping, or data-quality logic.

Character functions can be nested when each transformation serves a clear purpose. For example, the following expression first removes trailing blanks and then converts the remaining value to uppercase:

SELECT UPPER(RTRIM(status_code)) AS normalized_status
FROM   service_request;

Nesting order matters. RTRIM(UPPER(value), 'X') removes trailing uppercase X characters after conversion, whereas UPPER(RTRIM(value, 'X')) removes only the characters that match the original trim set before conversion. Keep expressions readable and test boundary cases, including nulls, empty strings, punctuation, accented characters, and values that contain only trim characters.

When a function appears regularly in a selective predicate, a function-based index can make the transformed expression searchable. For example, an application that repeatedly performs exact case-normalized lookups could use an index such as:

CREATE INDEX pet_log_user_upper_ix
    ON pet_care_log (UPPER(created_by_user));

A matching predicate can then use the indexed expression:

SELECT log_text
FROM   pet_care_log
WHERE  UPPER(created_by_user) = UPPER(:user_name);

This index does not make every character search efficient. In particular, it does not solve the leading-wildcard issue in LIKE '%text%'. Index creation also adds storage and write-maintenance cost. Confirm that the query expression matches the indexed expression, gather appropriate optimizer statistics, and compare execution plans with representative data before adopting the index.

Summary

  • UPPER supports binary uppercase conversion and can normalize both sides of a case-insensitive comparison.
  • NLS_UPPER is the corresponding choice when language-sensitive uppercase rules are required.
  • INITCAP formats words according to deterministic rules but should not be used as a universal personal-name correction tool.
  • NLS_INITCAP provides linguistic initial-capital conversion.
  • RTRIM removes trailing blanks by default or trailing members of a supplied character set.
  • SOUNDEX finds possible English sound-alike values; it does not prove that two names identify the same person.
  • Functions in predicates can affect index access, so review execution plans and consider purpose-built indexes only when the workload warrants them.

Character Functions - Quiz

Click the Quiz link below to test your knowledge of UPPER, INITCAP, RTRIM, and SOUNDEX.

Character Functions - Quiz

In the next lesson, you will learn how to format values with TO_CHAR and transform numeric or datetime values with ROUND and TRUNC.


SEMrush Software 5 SEMrush Banner 5