0
0
SQLquery~5 mins

Why string functions matter in queries in SQL

Choose your learning style9 modes available
Introduction
String functions help us change and check text in databases easily. They make searching and organizing text data simple and fast.
When you want to find all names starting with a certain letter.
When you need to clean up extra spaces from user input.
When you want to combine first and last names into one full name.
When you want to change text to all uppercase or lowercase for comparison.
When you want to extract a part of a text, like area code from a phone number.
Syntax
SQL
FUNCTION_NAME(column_name)
-- Example: UPPER(name)
-- Example: LENGTH(description)
String functions usually take a text column or value as input.
They return a new text or number based on the operation.
Examples
Changes all letters in the 'name' column to uppercase.
SQL
SELECT UPPER(name) FROM users;
Returns the number of characters in the 'description' column.
SQL
SELECT LENGTH(description) FROM products;
Joins first and last names with a space in between.
SQL
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
Removes extra spaces from the start and end of the 'email' column.
SQL
SELECT TRIM(email) FROM contacts;
Sample Program
This query creates a table and adds some customers. Then it selects their id, full name in uppercase, and email without extra spaces.
SQL
CREATE TABLE customers (
  id INT,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  email VARCHAR(100)
);

INSERT INTO customers VALUES
(1, 'Alice', 'Smith', ' alice@example.com '),
(2, 'Bob', 'Brown', 'bob@example.com'),
(3, 'Carol', 'Davis', ' carol@example.com');

SELECT id, CONCAT(UPPER(first_name), ' ', UPPER(last_name)) AS full_name, TRIM(email) AS clean_email FROM customers;
OutputSuccess
Important Notes
String functions can help fix messy data before using it.
Using string functions in queries can speed up searching and sorting.
Remember that some string functions may behave differently depending on the database system.
Summary
String functions let you change and check text data easily in queries.
They help clean, combine, and compare text for better results.
Using them makes working with text in databases faster and simpler.