Preloader

SQL String Functions

SQL
SQL Tutorials

SQL string functions help you work with text data stored in a database. You can use these functions to combine text, change letter case, find characters, replace words, and more. They are very useful when working with names, emails, addresses, and other text based fields.

String functions make data cleaner and easier to manage. For example, you can format user names, remove extra spaces, or extract part of an email address.

Common SQL String Functions

FunctionDescription
UPPER()Converts text to uppercase
LOWER()Converts text to lowercase
LENGTH()Returns the number of characters
SUBSTRING()Extracts part of a string
CONCAT()Joins two or more strings
TRIM()Removes extra spaces
REPLACE()Replaces part of a string
INSTR() / POSITION()Finds the position of a substring

1. UPPER() Function

This function converts all characters into uppercase.

SELECT UPPER(name) AS upper_name
FROM students;

It is useful when you want consistent formatting for display or comparison.

2. LOWER() Function

This function converts all characters into lowercase.

SELECT LOWER(email) AS lower_email
FROM users;

It helps when checking case insensitive values.

3. LENGTH() Function

The LENGTH function returns the total number of characters in a string.

SELECT LENGTH(name) AS name_length
FROM students;

You can use it to validate input length.

4. SUBSTRING() Function

The SUBSTRING function extracts part of a string.

SELECT SUBSTRING(name, 1, 3) AS short_name
FROM students;

This extracts the first three characters.

5. CONCAT() Function

The CONCAT function joins multiple strings together.

SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;

It is commonly used to create full names.

6. TRIM() Function

The TRIM function removes extra spaces from the beginning and end.

SELECT TRIM(name) AS clean_name
FROM students;

This improves data consistency.

7. REPLACE() Function

The REPLACE function substitutes part of a string with another value.

SELECT REPLACE(phone, '-', '') AS clean_phone
FROM customers;

This removes hyphens from phone numbers.

8. Finding Position of a Substring

Some databases use INSTR(), while others use POSITION().

SELECT INSTR(email, '@') AS at_position
FROM users;

This returns the position of the @ symbol.

Using String Functions with WHERE Clause

You can combine string functions with conditions.

SELECT *
FROM users
WHERE LENGTH(password) < 8;

This filters users with short passwords.

Important Notes

  • Function names may slightly differ depending on the database system.
  • String functions work only with text data types.
  • They help clean, format, and analyze textual data efficiently.

When to Use SQL String Functions

  • Formatting names and emails
  • Cleaning user input
  • Extracting specific text parts
  • Standardizing data
  • Validating text length

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *