Introduction to SQLSQL DatabaseSQL DatatypesSQL QueriesSQL DML StatementsSQL Constraints and IndexingSQL FunctionsStored Procedure and Triggers
1. SQL Functions
SQL functions perform operations on data in a database. They can be categorized into several types:
a) String Functions
These functions manipulate text data.
CONCAT()
: Concatenates two or more strings.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM Customers;
UPPER()
: Converts a string to uppercase.
SELECT UPPER(first_name) FROM Customers;
LOWER()
: Converts a string to lowercase.
SELECT LOWER(first_name) FROM Customers;
b) Date and Time Functions
These functions are used to manipulate and format date and time values.
NOW()
: Returns the current date and time.
SELECT NOW() AS current_time;
YEAR()
, MONTH()
, DAY()
: Extracts the year, month, or day from a date.
SELECT YEAR(order_date) AS order_year, MONTH(order_date) AS order_month FROM Orders;
c) Mathematical Functions
These functions perform calculations on numeric data.
ROUND()
: Rounds a number to a specified decimal place.
SELECT ROUND(price, 2) AS rounded_price FROM Products;
ABS()
: Returns the absolute value of a number.
SELECT ABS(balance) AS positive_balance FROM Accounts;