Functions
Functions
Functions
CEIL(): It returns the smallest integer value that is greater than or equal to a number.
FLOOR(): It returns the largest integer value that is less than or equal to a number.
ATAN2(): It returns the arctangent of the x and y coordinates, as an angle and expressed in
radians.
LCASE() This function is used to convert the given string into lower case.
Syntax: LENGTH('GeeksForGeeks');
Output: 13
LPAD() This function is used to make the given string of the given size by adding the given
symbol.
Syntax: LPAD('geeks', 8, '0');
Output: 000geeks
LTRIM() This function is used to cut the given sub string from the original string.
MID() This function is used to find a word from the given position and of the given
size.
Syntax: Mid ("geeksforgeeks", 6, 2);
Output: for
POSITION() This function is used to find position of the first occurrence of the given alphabet.
REPLACE() This function is used to cut the given string by removing the given sub string.
Syntax: REPLACE('123geeks123', '123');
Output: geeks
RPAD() This function is used to make the given string as long as the given size by
adding the given symbol on the right.
Syntax: RPAD('geeks', 8, '0');
Output: ‘geeks000’
RTRIM() This function is used to cut the given sub string from the original string.
RPAD() This function is used to make the given string as long as the given size by
adding the given symbol on the right.
Syntax: RPAD('geeks', 8, '0');
Output: ‘geeks000’
TRIM() This function is used to cut the given symbol from the string.
SUBSTR() This function is used to find a sub string from the a string from the given position.
SUBSTRING_INDEX() This function is used to find a sub string before the given symbol.
Output: 2024 | 09 | 17
Output: 2024-09-10
Syntax:
SELECT DATEDIFF(MINUTE, start_time,
end_time) AS duration_minutes
FROM tasks;
The SQL query provided is used to calculate the duration in minutes between two
timestamps, start_time and end_time, for tasks recorded in a tasks table.
The IFNULL and COALESCE
Functions
When working with databases, dealing with missing or unknown data is a common
challenge. In SQL, this is represented by NULL values. Handling NULL values
correctly is crucial for ensuring your data operations are accurate and consistent.
Two key functions that help manage NULL values are COALESCE and IFNULL.
While both functions aim to handle NULL values, they have different features and
uses.
IFNULL()
Syntax:
IFNULL(exp, replacement_value)
COALESCE()
Returns the first non-NULL value from the list of expressions. It is more
flexible as it can handle multiple expressions.
Syntax:
COALESCE(exp1, exp2, …, expN)
Example
Output:
Example of COALESCE ()
Query:
SELECT
emplTbl empID,
ename,
COALESCE(salary, bonus, 0) AS
updatedSalary
FROM
emplTbl;
Output:
The IF Function
IF( ) Function
The IF() function is a control flow function that returns different values
based on the result of a condition.
The IF() function in MySQL returns a value if the condition is TRUE and
another value if the condition is FALSE. The IF() function can return
values that can be either numeric or strings, depending upon the context
in which the function is used.
Parameters:
products
Query:
SELECT product_id, price,
IF(price > 100, 'Expensive', 'Affordable') AS category
FROM products;
Output:
The CASE Operator
The CASE statement in SQL is a versatile conditional expression that
enables us to incorporate conditional logic directly within our queries. It
is commonly used to generate new columns based on certain conditions
and provide custom values or control the output of our queries.
CASE case_value
WHEN condition THEN result1
WHEN condition THEN result2
…
Else result
END CASE;
Costumer
Query:
SELECT CustomerName, Age,
CASE
WHEN Country = "India" THEN 'Indian'
ELSE 'Foreign'
END AS Nationality
FROM Customer;
Output:
Reference:
THANK YOU :’>