?? Day 34: Exploring Practical SQL Implementation - Part 6??
JIGNESH KUMAR
Electrical & Instrumentation Engineer at SIC | Data Science Enthusiastic | ICE' 2024 SLIET
A. Common Functions in SQL:
Common functions are essential tools in SQL for manipulating and transforming data. They offer a range of functionalities, from simple tasks like finding string lengths or positions to more complex operations like converting data types or performing mathematical rounding. Understanding these functions is fundamental for efficient and precise data handling in SQL queries.
Real-Life Example:
Example Scenario:
| customer_id | customer_name |
|-------------|-----------------|
| 1 | Jignesh Kumar |
| 2 | Mahesh Kumar |
SELECT customer_name, LENGTH(customer_name) AS name_length
FROM customers;
| customer_name | name_length |
|-----------------|-------------|
| Jignesh Kumar | 13 |
| Mahesh Kumar | 12 |
2. INSTR(string, substring): Returns the position of the substring within the specified string.
Example Scenario:
| product_id | product_name |
|------------|--------------------|
| 1 | Laptop Dell XPS |
| 2 | Monitor ASUS 27" |
SELECT product_name, INSTR(product_name, 'ASUS') AS position
FROM products;
| product_name | position |
|--------------------|----------|
| Laptop Dell XPS | 0 |
| Monitor ASUS 27" | 9 |
3. CAST(expression AS datatype): Converts an expression into the specified data type.
Example Scenario:
| order_id | order_date |
|----------|------------------|
| 1 | '2024-01-15' |
| 2 | '2024-02-20' |
SELECT order_id, CAST(order_date AS DATE) AS formatted_date
FROM orders;
| order_id | formatted_date |
|----------|-----------------|
| 1 | 2022-01-15 |
| 2 | 2022-02-20 |
4. ADDDATE(input_date, days): Adds a number of days to a specified date.
Example Scenario:
| subscription_id | start_date |
|------------------|--------------|
| 1 | '2022-01-01' |
| 2 | '2022-02-15' |
SELECT subscription_id, ADDDATE(start_date, 30) AS new_end_date
FROM subscriptions;
| subscription_id | new_end_date |
|------------------|--------------|
| 1 | 2022-01-31 |
| 2 | 2022-03-16 |
5. NOW: Returns the current date, including time.
Example Scenario:
SELECT NOW() AS current_datetime;
| current_datetime |
|-------------------------|
| 2024-02-14 14:30:45 |
6. CEILING(input_val): Returns the smallest integer greater than the provided number.
Example Scenario:
领英推荐
SELECT CEILING(15.25) AS rounded_up;
| rounded_up |
|------------|
| 16 |
7. FLOOR(input_val): Returns the largest integer less than the provided number.
Example Scenario:
SELECT FLOOR(15.75) AS rounded_down;
| rounded_down |
|--------------|
| 15 |
8. ROUND(input_val, [round_to]): Rounds a number to a specified number of decimal places.
Example Scenario:
SELECT ROUND(123.4567, 2) AS rounded_value;
| rounded_value |
|---------------|
| 123.46 |
9. TRUNCATE(input_value, num_decimals): Truncates a number to a number of decimals.
Example Scenario:
SELECT TRUNCATE(123.4567, 1) AS truncated_value;
| truncated_value |
|------------------|
| 123.4 |
10. REPLACE(whole_string, string_to_replace, replacement_string): Replaces one string inside the whole string with another string.
Example Scenario:
| address_id | full_address |
|------------|---------------------------|
| 1 | 123 Main Street, City A |
| 2 | 456 Oak Avenue, City B |
SELECT address_id, REPLACE(full_address, 'Street', 'St.') AS modified_address
FROM addresses;
| address_id | modified_address |
|------------|-------------------------------|
| 1 | 123 Main St., City A |
| 2 | 456 Oak Avenue, City B |
11. SUBSTRING(string, start_position): Returns part of a value based on a position and length.
Example Scenario:
| product_id | product_description |
|------------|-----------------------------------|
| 1 | High-performance Laptop |
| 2 | Large Screen 4K Gaming Monitor |
SELECT product_id, SUBSTRING(product_description, 1, 20) AS short_description
FROM products;
| product_id | short_description |
|------------|---------------------------------|
| 1 | High-performance La |
| 2 | Large Screen 4K Gami |