What are String Functions?
String Function Examples
| Function | Input | Output |
|---|---|---|
| UPPER(name) | john | JOHN |
| LOWER(name) | MARY | mary |
| LENGTH(name) | Peter | 5 |
| CONCAT(first, last) | John + Doe | John Doe |
4 rows
String functions help you work with text. You can change text to uppercase, combine names, get text length, and more.
Simple analogy: Like text tools - make text LOUD (uppercase) or quiet (lowercase).
Common String Functions
String Function Examples
| Function | Input | Output |
|---|---|---|
| UPPER(name) | john | JOHN |
| LOWER(name) | MARY | mary |
| LENGTH(name) | Peter | 5 |
| CONCAT(first, last) | John + Doe | John Doe |
4 rows
UPPER - Make Text Uppercase
SELECT UPPER(name) FROM students;Example:
- Input: john
- Output: JOHN
LOWER - Make Text Lowercase
SELECT LOWER(name) FROM students;Example:
- Input: MARY
- Output: mary
CONCAT - Combine Text
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM users;Example:
- Input: John + Doe
- Output: John Doe
LENGTH - Count Characters
SELECT name, LENGTH(name) AS name_length
FROM students;Example:
- Input: John
- Output: 4
SUBSTRING - Get Part of Text
SELECT SUBSTRING(email, 1, 4) AS email_start
FROM users;Example:
- Input: john@email.com
- Output: john
TRIM - Remove Spaces
SELECT TRIM(name) FROM students;Example:
- Input: " John "
- Output: "John"
Real Examples
String Function Examples
| Function | Input | Output |
|---|---|---|
| UPPER(name) | john | JOHN |
| LOWER(name) | MARY | mary |
| LENGTH(name) | Peter | 5 |
| CONCAT(first, last) | John + Doe | John Doe |
4 rows
Example 1: Format Names
SELECT
UPPER(first_name) AS first,
LOWER(last_name) AS last
FROM users;Example 2: Create Email
SELECT
name,
LOWER(name) || '@company.com' AS email
FROM employees;Result: John becomes john@company.com
Example 3: Find Long Names
SELECT name
FROM students
WHERE LENGTH(name) > 10;Shows students with names longer than 10 characters.
Summary
String Function Examples
| Function | Input | Output |
|---|---|---|
| UPPER(name) | john | JOHN |
| LOWER(name) | MARY | mary |
| LENGTH(name) | Peter | 5 |
| CONCAT(first, last) | John + Doe | John Doe |
4 rows
Common functions:
- UPPER: Make uppercase
- LOWER: Make lowercase
- CONCAT: Combine text
- LENGTH: Count characters
- SUBSTRING: Get part
- TRIM: Remove spaces