Skip to main content

✅ SQL String Functions

 

✅ 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 💯

Comments

Popular posts from this blog

Oracle Object Oriented Concepts

An Object is a reusable application component that developers need to be aware of, rather than how it works. Object are basic entities in a system. They could represent a person, place, bank account, or any item that is handled by program. Every object consists of an attribute and one or more methods. An attribute could be any property of the object. Class:           It is a collection of attributes and functions (method) to plan the object. Object Table : ·    Object table are created by using the user defined data types. ·        In an object table each row or record is treated as an object. ·        Each row in an object table has an object Identified (OID), which is unique through out       the   database. ·       The rows or objects of an object table can be referenced by other objects with in the  database. ·    ...

About Oracle Buffer

About Oracle Buffer :- All Commands of SQL are typed at the SQL prompt. Only one SQL statement is managed in the SQL buffer. The current SQL statement replaces the previous SQL statement in the buffer. The SQL statements can be divided into different lines within the SQL buffer. Only one line i.e, the current line can be active at a time in the SQL buffer. At SQL prompt, editing is possible only in the current SQL buffer line. Every statement of SQL should be terminated using semi colon " ; ". One sql statement can contain only one semi colon. To run the previous or current SQL statement in the bufer type " / " at SQL prompt. To open the SQL editor type " ED " at SQL prompt.

Filtering and Sorting data using SQL statements

  Filtering & Sorting Data using SQL     Once you know the basics ( SELECT , FROM , WHERE ), filtering and sorting helps you get the exact rows from tables .   1. Filtering with Conditions ( AND , OR , NOT )  - Use WHERE to filter rows. - Combine with logical operators :     • AND: all conditions true      • OR: any condition true      • NOT: negate condition   - - All conditions true (AND) SELECT *  FROM employees WHERE department = 'Sales'    AND salary > 50000; -- Any condition true (OR) SELECT *  FROM employees WHERE department = 'Sales'     OR department = 'Marketing'; -- Negate condition (NOT) SELECT *  FROM employees WHERE NOT department = 'HR';   2. Pattern Matching with LIKE   - % = any sequence, _ = single char     • Names starting with 'J'      • Names with 4 letters starting with 'J'   - - N...