Functions for string transformation

Functions for Manipulating Data in SQL Server

Ana Voicu

Data Engineer

LOWER() and UPPER()

LOWER(character_expression)

  • Converts all characters from a string to lowercase.

UPPER(character_expression)

  • Converts all characters from a string to uppercase.
Functions for Manipulating Data in SQL Server

LOWER() and UPPER() example

SELECT 
    country,
    LOWER(country) AS country_lowercase,
    UPPER(country) AS country_uppercase
FROM voters;
| country | country_lowercase | country_uppercase |
|---------|-------------------|-------------------|
| Denmark | denmark           | DENMARK           |
| France  | france            | FRANCE            |
| Belgium | belgium           | BELGIUM           |
Functions for Manipulating Data in SQL Server

LEFT() and RIGHT()

LEFT(character_expression, number_of_characters)

  • Returns the specified number of characters from the beginning of the string

RIGHT(character_expression, number_of_characters)

  • Returns the specified number of characters from the end of the string
Functions for Manipulating Data in SQL Server

LEFT() and RIGHT() example

SELECT 
    country,
    LEFT(country, 3) AS country_prefix,
    email,
    RIGHT(email, 4) AS email_domain
FROM voters;
| country | country_prefix | email              | email_domain |
|---------|----------------|--------------------|--------------|
| Denmark | Den            | [email protected]   | .com         |
| France  | Fra            | [email protected]     | .com         |
| Belgium | Bel            | [email protected] | .com         |
Functions for Manipulating Data in SQL Server

LTRIM(), RTRIM(), and TRIM()

LTRIM(character_expression)

  • Returns a string after removing the leading blanks.

RTRIM(character_expression)

  • Returns a string after removing the trailing blanks.

TRIM([characters FROM] character_expression)

  • Returns a string after removing the blanks or other specified characters.
Functions for Manipulating Data in SQL Server

REPLACE()

REPLACE(character_expression, searched_expression, replacement_expression)

  • Returns a string where all occurrences of an expression are replaced with another one.
SELECT REPLACE('I like apples, apples are good.', 'apple', 'orange') AS result;
| result                          | 
|---------------------------------|
|I like oranges, oranges are good.|
Functions for Manipulating Data in SQL Server

SUBSTRING()

SUBSTRING(character_expression, start, number_of_characters)

  • Returns part of a string.
SELECT SUBSTRING('123456789', 5, 3) AS result;
| result | 
|--------|
| 567    |
Functions for Manipulating Data in SQL Server

Let's practice!

Functions for Manipulating Data in SQL Server

Preparing Video For Download...