๐ฅNow, letโs move to the next topic:
โ
SQL String Functions
๐ง 1. What are String Functions?
String functions are used to
๐ manipulate text data
๐ clean messy data
๐ format outputs
Used heavily in:
โ Data Analytics
โ Reporting
โ ETL processes
โก 2. Common String Functions
Function : Purpose
UPPER() : Convert to uppercase
LOWER() : Convert to lowercase
LENGTH() : Count characters
CONCAT() : Join strings
SUBSTRING() : Extract part of string
TRIM() : Remove spaces
REPLACE() : Replace text
๐ฅ 3. UPPER() & LOWER()
SELECT UPPER(name) AS upper_name
FROM employees;
SELECT LOWER(name) AS lower_name
FROM employees;
๐ฅ 4. LENGTH()
๐ Count number of characters
SELECT name, LENGTH(name) AS total_chars
FROM employees;
๐ฅ 5. CONCAT()
๐ Combine strings
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
๐ฅ 6. SUBSTRING()
๐ Extract part of string
SELECT SUBSTRING(name, 1, 3)
FROM employees;
โ Extracts first 3 characters
๐ฅ 7. TRIM()
๐ Remove extra spaces
SELECT TRIM(' SQL ');
โ Result โ SQL
๐ฅ 8. REPLACE()
๐ Replace text inside string
SELECT REPLACE('I love Java', 'Java', 'SQL');
โ Result โ I love SQL
๐ฏ 9. Practice Tasks
1. Convert names to uppercase
2. Convert emails to lowercase
3. Combine first & last names
4. Extract first 4 letters of names
5. Remove extra spaces from city names
โก Mini Challenge ๐ฅ
๐ Create employee usernames using:
first 3 letters of name + employee ID
Example:
Amit + 101 โ Ami101
๐ฅ Mini Challenge Solution ๐ฏ
๐ Requirement:
Create username using:
โข First 3 letters of name
โข Employee ID
Example:
Amit + 101 โ Ami101
โ
SQL Solution
SELECT name,
emp_id,
CONCAT(SUBSTRING(name, 1, 3), emp_id) AS username
FROM employees;
โ
Example Output
name : emp_id : username
Amit : 101 : Ami101
Neha : 102 : Neh102
Ravi : 103 : Rav103
๐ง How It Works
๐ SUBSTRING(name, 1, 3)
Extracts first 3 letters
๐ CONCAT()
Combines extracted text with employee ID
๐ฅ Real-World Usage:
String functions are commonly used for:
๐ Username generation
๐ Email formatting
๐ Data cleaning
๐ Customer IDs ๐ฏ
Double Tap โค๏ธ For More
Post #2482
3.11K
- โค 8
- ๐ 1