Database Administration — Lecture 05
Definition
Single-row functions are Oracle SQL functions that operate on each individual row and return one result for each row.
For example, if a query returns 10 employees, a single-row function can produce 10 corresponding results.
Important correction: Single-row functions are not the same as group functions. Group functions such as
SUM(),AVG(), andCOUNT()combine multiple rows and normally return one result for a group.
Single-row functions can be used in:
SELECTWHERE- Other SQL expressions
They can accept:
- Column values
- Literals
- Expressions
- Other functions
Key Points
1. Case Conversion Functions
| Function | Purpose | Example | Result |
|---|---|---|---|
LOWER() | Converts all characters to lowercase | LOWER('SMITH') | smith |
UPPER() | Converts all characters to uppercase | UPPER('smith') | SMITH |
INITCAP() | Capitalizes the first letter of each word | INITCAP('john smith') | John Smith |
LOWER()
Converts a string to lowercase.
LOWER('SMITH')
Result:
smith
UPPER()
Converts a string to uppercase.
UPPER('smith')
Result:
SMITH
INITCAP()
Converts the first character of each word to uppercase and the remaining characters to lowercase.
INITCAP('john smith')
Result:
John Smith
2. Character Manipulation Functions
| Function | Purpose |
|---|---|
CONCAT() | Joins two strings |
SUBSTR() | Extracts part of a string |
LENGTH() | Returns the number of characters |
INSTR() | Finds the position of a substring |
LPAD() | Adds characters to the left |
RPAD() | Adds characters to the right |
TRIM() | Removes specified leading/trailing characters |
Example / Code
CONCAT()
Joins two strings.
SELECT CONCAT('John', 'Doe') FROM dual;
Result:
JohnDoe
For more than two strings, Oracle commonly uses the concatenation operator ||:
SELECT 'John' || ' ' || 'Doe' FROM dual;
Result:
John Doe
SUBSTR()
Extracts a portion of a string.
Syntax:
SUBSTR(string, start_position, length)
Example:
SELECT SUBSTR('Good', 3, 2) FROM dual;
Result:
od
Correction: Your note says
SUBSTR(string, start, end). The third argument is length, not the ending position.
Another example:
SELECT SUBSTR('Database', 1, 4) FROM dual;
Result:
Data
LENGTH()
Returns the number of characters.
SELECT LENGTH('Good') FROM dual;
Result:
4
INSTR()
Returns the position of a substring inside another string.
SELECT INSTR('Good', 'o') FROM dual;
Result:
2
Because:
G o o d
1 2 3 4
Correction:
INSTR()does not count how many times a character occurs. It returns the position of the first occurrence by default.
For example:
SELECT INSTR('Database', 'a') FROM dual;
Result:
2
LPAD()
Adds characters to the left side until the string reaches the specified total length.
SELECT LPAD('King', 20, '*') FROM dual;
Result:
****************King
Here, 20 means the final total length, not the number of * characters to add.
RPAD()
Adds characters to the right side.
SELECT RPAD('King', 10, '*') FROM dual;
Result:
King******
TRIM()
Removes specified characters from the beginning and/or end of a string.
SELECT TRIM('$' FROM '$2,345$') FROM dual;
Result:
2,345
For removing spaces:
SELECT TRIM(' Hello ') FROM dual;
Result:
Hello
Case Conversion in WHERE
One important use of LOWER() is when the case of stored data is uncertain.
SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'smith';
This can find:
SMITH
Smith
smith
sMiTh
because the column value is converted to lowercase before comparison.
Similarly:
SELECT employee_id, last_name, department_id
FROM employees
WHERE UPPER(last_name) = 'SMITH';
Explanation
Think of single-row functions as functions that work row by row.
Suppose the table contains:
| LAST_NAME |
|---|
| SMITH |
| KING |
| JONES |
If you use:
SELECT LOWER(last_name)
FROM employees;
Oracle applies LOWER() separately to each row:
SMITH → smith
KING → king
JONES → jones
Therefore, the function produces one result for every row.
Easy way to remember the character functions
LOWER → lowercase
UPPER → UPPERCASE
INITCAP → First Letter
CONCAT → Join
SUBSTR → Extract
LENGTH → Count characters
INSTR → Find position
LPAD → Add to LEFT
RPAD → Add to RIGHT
TRIM → Remove from edges
Output
For:
SELECT
LOWER('INTRO TO SQL') AS lower_text,
UPPER('Intro to SQL') AS upper_text,
INITCAP('intro to sql') AS initcap_text
FROM dual;
The result is approximately:
| LOWER_TEXT | UPPER_TEXT | INITCAP_TEXT |
|---|---|---|
| intro to sql | INTRO TO SQL | Intro To Sql |
For:
SELECT
CONCAT('Good', 'Day'),
SUBSTR('Good', 3, 2),
LENGTH('Good'),
INSTR('Good', 'o')
FROM dual;
Result:
| CONCAT | SUBSTR | LENGTH | INSTR |
|---|---|---|---|
| GoodDay | od | 4 | 2 |
Common Mistakes
-
Confusing single-row functions with group functions
Single-row → one result per row Group → combines multiple rows -
Thinking
SUBSTR()uses an ending positionSUBSTR('Good', 3, 2)2means length, not ending position. -
Thinking
INSTR()counts occurrencesINSTR()returns a position.INSTR('Good', 'o') → 2 -
Confusing
LPAD()andRPAD()LPAD → left RPAD → right -
Thinking the number in
LPAD()is the number of padding charactersLPAD('King', 10, '*')means the final string length is 10.
-
Thinking
TRIM()only removes spacesTRIM()can also remove a specified character from the beginning/end. -
Using
CONCAT()for many stringsFor multiple pieces,
||is often clearer:first_name || ' ' || last_name
Short Exam Notes
- Single-row function: operates on each row → one result per row.
- Group function: operates on groups of rows → one result per group.
LOWER()→ lowercase.UPPER()→ uppercase.INITCAP()→ first letter of each word uppercase.CONCAT()→ joins strings.SUBSTR()→ extracts part of a string.SUBSTR(string, start, length).LENGTH()→ number of characters.INSTR()→ position of a substring.LPAD()→ padding on the left.RPAD()→ padding on the right.TRIM()→ removes characters/spaces from string edges.INSTR()finds a position, not the number of occurrences.