Explain any four String functions with example.
Of course. Here is an explanation of four common and essential SQL string functions, complete with their purpose, syntax, and practical examples.
String functions in SQL are built-in functions that allow you to manipulate character or text data. They are used for a wide variety of tasks, such as cleaning data, formatting output, extracting information, and preparing data for joins or comparisons.
To demonstrate these functions, let's use the following Customers
table:
Customers
Table:
| CustomerID | FirstName | LastName | Email |
| :--- | :--- | :--- | :--- |
| 1 | John | Smith | john.smith@example.com |
| 2 | Mary | Jones | Mary.J@email.org |
| 3 | Peter | Williams | peter@mail.net |
CONCAT()
CONCAT()
function is used to join two or more strings together end-to-end, creating a single, combined string. This is also known as concatenation.Syntax:
`
sql
CONCAT(string1, string2, ..., stringN)
`
Note: Some database systems use different syntax for concatenation, such as the +
operator (in SQL Server) or the ||
operator (in Oracle and PostgreSQL).
Example:
Let's create a full name for each customer by combining their FirstName
and LastName
with a space in between.
`
sql
SELECT
FirstName,
LastName,
CONCAT(FirstName, ' ', LastName) AS FullName
FROM Customers;
`
Result:
| FirstName | LastName | FullName |
| :--- | :--- | :--- |
| John | Smith | John Smith |
| Mary | Jones | Mary Jones |
| Peter | Williams | Peter Williams |
UPPER()
and LOWER()
Purpose: These functions are used to convert the case of a string.
UPPER()
converts all characters in a string to UPPERCASE.
LOWER()
converts all characters in a string to lowercase.
These are extremely useful for performing case-insensitive searches or for standardizing data entry.
Syntax:
`
sql
UPPER(string)
LOWER(string)
`
Example:
Let's standardize the email addresses by converting them all to lowercase to ensure consistency.
`
sql
SELECT
Email,
LOWER(Email) AS StandardizedEmail
FROM Customers;
`
Result:
| Email | StandardizedEmail |
| :--- | :--- |
| john.smith@example.com | john.smith@example.com |
| Mary.J@email.org | mary.j@email.org |
| peter@mail.net | peter@mail.net |
LENGTH()
(or LEN()
)Syntax:
In MySQL, PostgreSQL, and Oracle: LENGTH(string)
In SQL Server: LEN(string)
Example:
Let's find the length of each customer's last name.
`
sql
-- Using LENGTH for MySQL/PostgreSQL/Oracle
SELECT
LastName,
LENGTH(LastName) AS NameLength
FROM Customers;
`
Result:
| LastName | NameLength |
| :--- | :--- |
| Smith | 5 |
| Jones | 5 |
| Williams | 8 |
SUBSTRING()
(or SUBSTR()
)SUBSTRING()
function extracts a part of a string. You specify the string, a starting position, and the number of characters you want to extract.Syntax:
`
sql
SUBSTRING(string, start_position, number_of_characters)
`
Note: The start_position
is 1-based, meaning the first character of the string is at position 1.
Example:
Let's create a user code for each customer consisting of the first three letters of their last name.
`
sql
SELECT
LastName,
SUBSTRING(LastName, 1, 3) AS UserCode
FROM Customers;
`
Result:
| LastName | UserCode |
| :--- | :--- |
| Smith | Smi |
| Jones | Jon |
| Williams | Wil |