๐ŸŽ“ A1Learner
๐Ÿ”

SQL String Functions

String functions are functions that work on text values. UPPER, LOWER and LENGTH are three common string functions.

Uppercase names

UPPER sits inside SELECT, wrapped around the name column. Every name comes back in uppercase letters.

SELECT name, UPPER(name) AS loud_name
FROM customers
ORDER BY customer_id;

Lowercase countries

LOWER does the opposite job. The country values come back in lowercase letters.

SELECT name, LOWER(country) AS small_country
FROM customers
ORDER BY customer_id
LIMIT 3;

Counting characters

LENGTH counts the characters in each name. The space between first and last name counts as one character.

SELECT name, LENGTH(name) AS name_length
FROM customers
ORDER BY customer_id;

Example

SELECT UPPER(city) AS loud_city, LENGTH(name) AS name_length
FROM customers
WHERE city IS NOT NULL
ORDER BY customer_id
LIMIT 4;

Key points

  • UPPER: a function that returns a text value in uppercase letters
  • LOWER: a function that returns a text value in lowercase letters
  • LENGTH: a function that returns the number of characters in a text value
  • Use a string function inside SELECT to change what a column returns.
  • A string function changes the result only.
  • The stored value stays as it was.

In this example, UPPER changes each city to uppercase letters. LENGTH counts the characters in each customer name. The WHERE clause skips the customer whose city is missing. The LIMIT clause keeps the first four rows.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท DB Fiddle

Exercise

Which function returns the number of characters in a text value?