When you have text data in your Oracle database that is messy—like all lowercase or all uppercase—it can look unprofessional in reports. The INITCAP function is a simple and powerful tool to fix this.
This guide will explain what the Oracle INITCAP function is, its syntax, and how to use it with practical examples to clean up your data.
What is the INITCAP Function in Oracle?
The INITCAP function (which stands for "Initial Capitalization") converts a string into "title case."
It works by following two simple rules:
- It makes the first letter of every word uppercase.
- It makes all other letters lowercase.
Oracle considers a "word" to be any set of letters separated by a space or any character that isn't a letter or number (like a hyphen -, a period ., or a comma ,).
INITCAP Function Syntax
The syntax for the INITCAP function is very simple:
INITCAP(char)
Let's break that down:
char: This is the string or column name you want to format. It can be aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type.
Oracle INITCAP Function Examples
Here are two practical examples of how to use INITCAP in your Oracle SQL queries.
Example 1: Cleaning Up a Messy String with INITCAP
This is the most direct way to use INITCAP. Imagine you have a string that is a mix of all caps and all lowercase. INITCAP will clean it up perfectly into title case.
We use the DUAL table for this test, which is a special one-row, one-column table in Oracle.
Query:
SELECT
INITCAP('the quick BROWN fox JUMPS over the LAZY dog') AS Cleaned_String
FROM DUAL;
Result:
CLEANED_STRING --------------------------------------------- The Quick Brown Fox Jumps Over The Lazy Dog
Notice how it corrected both BROWN (uppercase) and the (lowercase) automatically.
Example 2: Using INITCAP Formatting Data from a Table Column
A more common use is to clean up data stored in a table. Imagine you have a products table with a product_name column that has inconsistent capitalization.
Let's use UNION ALL to simulate a table with messy data:
Query:
-- This query mimics a table with two messy product names
-- and shows how INITCAP cleans them up.
SELECT
'apple iPHONE 15 PRO' AS original_name,
INITCAP('apple iPHONE 15 PRO') AS clean_name
FROM DUAL
UNION ALL
SELECT
'SAMSUNG galaxy BOOK 4',
INITCAP('SAMSUNG galaxy BOOK 4')
FROM DUAL;
Result:
ORIGINAL_NAME CLEAN_NAME ------------------------ ------------------------ apple iPHONE 15 PRO Apple Iphone 15 Pro SAMSUNG galaxy BOOK 4 Samsung Galaxy Book 4
As you can see, this is perfect for making data from a column look consistent and professional for reports and applications.
