Lesson 39 +10 XP

String Functions

SQL has many functions for working with text.

UPPER and LOWER


sql
SELECT UPPER(CustomerName) FROM Customers; - "ALFREDS FUTTERKISTE"

### SELECT LOWER(City) FROM Customers; - "berlin"

LENGTH / LEN

MySQL uses LENGTH, SQL Server uses LEN:


sql

### SELECT LENGTH(City) FROM Customers; - MySQL

### SELECT LEN(City) FROM Customers; - SQL Server

CONCAT

Combine strings:


sql
SELECT CONCAT(FirstName, ' ', LastName) AS FullName

### FROM Persons;

SUBSTRING / SUBSTR

Take part of a string:


sql
SELECT SUBSTRING(City, 1, 3) FROM Customers; - first 3 characters

TRIM

Remove spaces from both ends:


sql

### SELECT TRIM('   Hello   ') AS Clean; - 'Hello'

REPLACE


sql
SELECT REPLACE('SQL Tutorial', 'SQL', 'MySQL'); - 'MySQL Tutorial'

Note: function names vary

MySQL uses LENGTH; SQL Server uses LEN. Always check your database's reference.

TL;DR

  • UPPER / LOWER change text case.
  • CONCAT joins strings; LENGTH/LEN measures them.
  • SUBSTRING cuts text; TRIM removes spaces; REPLACE swaps text.
  • Function names differ slightly between databases.