Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

String Functions - CONCAT, LENGTH, LOWER, UPPER, SUBSTRING, TRIM in SQL


SQL provides various string functions that are used to manipulate string data types. These functions allow you to combine, modify, and clean up text values in your database. In this article, we will explore some of the most commonly used string functions in SQL: CONCAT, LENGTH, LOWER, UPPER, SUBSTRING, and TRIM.

1. CONCAT - Concatenate Strings

The CONCAT function is used to join two or more strings together into a single string. This is useful when you need to combine multiple pieces of text data from different columns or values.

Syntax:

      SELECT CONCAT(string1, string2, ...)
      FROM table_name;
    

CONCAT takes one or more string arguments and returns a single string that is the result of concatenating them.

Example: Using CONCAT

      -- Concatenate first and last names of employees
      SELECT CONCAT(FirstName, ' ', LastName) AS FullName
      FROM Employees;
    

This query concatenates the FirstName and LastName columns of the Employees table to create a full name.

2. LENGTH - Find Length of a String

The LENGTH function is used to find the number of characters in a string, including spaces. It returns an integer value that represents the length of the string.

Syntax:

      SELECT LENGTH(string)
      FROM table_name;
    

LENGTH returns the number of characters in the given string.

Example: Using LENGTH

      -- Find the length of employee names
      SELECT FirstName, LENGTH(FirstName) AS NameLength
      FROM Employees;
    

This query retrieves the first name and its length (in terms of characters) for each employee in the Employees table.

3. LOWER - Convert String to Lowercase

The LOWER function is used to convert all characters in a string to lowercase. This is useful when you need to standardize text data.

Syntax:

      SELECT LOWER(string)
      FROM table_name;
    

LOWER converts all characters in the string to lowercase.

Example: Using LOWER

      -- Convert employee names to lowercase
      SELECT LOWER(FirstName) AS LowerCaseName
      FROM Employees;
    

This query converts the FirstName column values to lowercase for all employees in the Employees table.

4. UPPER - Convert String to Uppercase

The UPPER function is used to convert all characters in a string to uppercase. This function is commonly used for consistency when dealing with case-sensitive data.

Syntax:

      SELECT UPPER(string)
      FROM table_name;
    

UPPER converts all characters in the string to uppercase.

Example: Using UPPER

      -- Convert employee names to uppercase
      SELECT UPPER(FirstName) AS UpperCaseName
      FROM Employees;
    

This query converts the FirstName column values to uppercase for all employees in the Employees table.

5. SUBSTRING - Extract Part of a String

The SUBSTRING function is used to extract a specific portion of a string, based on a starting position and length. This is useful when you need to work with parts of a string, such as extracting the area code from a phone number.

Syntax:

      SELECT SUBSTRING(string, start_position, length)
      FROM table_name;
    

SUBSTRING extracts a portion of the string starting at the specified position and continuing for the specified length.

Example: Using SUBSTRING

      -- Extract the first 3 characters from the employee's first name
      SELECT SUBSTRING(FirstName, 1, 3) AS NamePart
      FROM Employees;
    

This query extracts the first 3 characters from the FirstName column for each employee in the Employees table.

6. TRIM - Remove Leading and Trailing Spaces

The TRIM function is used to remove leading and trailing spaces from a string. This is helpful when dealing with user input that may have unwanted spaces at the beginning or end.

Syntax:

      SELECT TRIM(string)
      FROM table_name;
    

TRIM removes spaces from both the beginning and the end of the string, but not from the middle of the string.

Example: Using TRIM

      -- Remove leading and trailing spaces from employee names
      SELECT TRIM(FirstName) AS TrimmedName
      FROM Employees;
    

This query removes any extra spaces before and after the FirstName values for employees in the Employees table.

7. Conclusion

SQL string functions such as CONCAT, LENGTH, LOWER, UPPER, SUBSTRING, and TRIM are essential for manipulating and analyzing textual data. These functions allow you to clean, standardize, and extract relevant information from string columns in your database. Understanding how to use these functions effectively will improve your ability to work with text-based data in SQL.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java