All 40 lessons

Lesson 9 of 40 · SQL Functions

SQL String Functions

By Skillancy Editorial Team

Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.

String functions let you clean, combine and extract text inside a query. Analysts use them constantly: standardising city names, pulling the domain out of an email, or building a display label.

Quick reference

FunctionWhat it doesExampleResult
UPPER(s) / LOWER(s)change caseUPPER('delhi')DELHI
INITCAP(s)capitalise each word (PostgreSQL)INITCAP('new delhi')New Delhi
LENGTH(s)number of charactersLENGTH('Pune')4
CONCAT(a, b, ...) or a || bjoin text'Shop' || 'Kart'ShopKart
SUBSTRING(s, start, len)part of a stringSUBSTRING('ShopKart', 5, 4)Kart
LEFT(s, n) / RIGHT(s, n)first / last n charactersRIGHT('9876543210', 4)3210
TRIM(s)remove leading/trailing spacesTRIM(' UPI ')UPI
REPLACE(s, from, to)replace textREPLACE('a-b', '-', ' ')a b
POSITION(x IN s)where x starts (0 if absent)POSITION('@' IN 'a@b.com')2
SPLIT_PART(s, d, n)nth piece after splitting (PostgreSQL)SPLIT_PART('a@b.com', '@', 2)b.com

Examples

1. Change case

SELECT customer_name,
       UPPER(customer_name) AS upper_name,
       LOWER(city) AS city_lower
FROM customers
LIMIT 3;
Try it in the SQL Compiler

Result:

customer_nameupper_namecity_lower
Aarav SharmaAARAV SHARMAbengaluru
Priya NairPRIYA NAIRkochi
Rohan GuptaROHAN GUPTAdelhi

(3 rows)

2. Combine columns

Build a label like "Aarav Sharma (Bengaluru)":

SELECT customer_name || ' (' || city || ')' AS customer_label
FROM customers
LIMIT 3;
Try it in the SQL Compiler

Result:

customer_label
Aarav Sharma (Bengaluru)
Priya Nair (Kochi)
Rohan Gupta (Delhi)

(3 rows)

CONCAT does the same and is safer with NULLs, because it treats them as empty text:

SELECT customer_name,
       customer_name || ' - ' || phone AS with_pipe,
       CONCAT(customer_name, ' - ', phone) AS with_concat
FROM customers
WHERE phone IS NULL;
Try it in the SQL Compiler

Result:

customer_namewith_pipewith_concat
Rohan GuptaNULLRohan Gupta -
Karan MehtaNULLKaran Mehta -

(2 rows)

3. Extract part of a string

The first name is everything before the space:

SELECT customer_name,
       SPLIT_PART(customer_name, ' ', 1) AS first_name,
       LEFT(customer_name, 1) AS initial
FROM customers
LIMIT 4;
Try it in the SQL Compiler

Result:

customer_namefirst_nameinitial
Aarav SharmaAaravA
Priya NairPriyaP
Rohan GuptaRohanR
Sneha ReddySnehaS

(4 rows)

4. Get the email domain

SELECT email,
       SUBSTRING(email, POSITION('@' IN email) + 1) AS domain
FROM customers
WHERE email IS NOT NULL
LIMIT 5;
Try it in the SQL Compiler

Result:

emaildomain
aarav.sharma@gmail.comgmail.com
priya.nair@yahoo.comyahoo.com
rohan.g@outlook.comoutlook.com
sneha.reddy@gmail.comgmail.com
vikram.singh@gmail.comgmail.com

(5 rows)

SUBSTRING with no length returns everything from the start position to the end.

5. Mask a phone number

Show only the last four digits, as you would on an order confirmation:

SELECT customer_name,
       'XXXXXX' || RIGHT(phone, 4) AS masked_phone
FROM customers
WHERE phone IS NOT NULL
LIMIT 3;
Try it in the SQL Compiler

Result:

customer_namemasked_phone
Aarav SharmaXXXXXX3210
Priya NairXXXXXX2345
Sneha ReddyXXXXXX6655

(3 rows)

6. Clean messy text

Real data often arrives with extra spaces and inconsistent case. TRIM + INITCAP fixes it:

SELECT '  new delhi ' AS raw_value,
       INITCAP(TRIM('  new delhi ')) AS cleaned,
       LENGTH('  new delhi ') AS raw_length;
Try it in the SQL Compiler

Result:

raw_valuecleanedraw_length
new delhiNew Delhi12

(1 row)

7. Replace text

SELECT product_name,
       REPLACE(product_name, ' ', '-') AS url_slug
FROM products
LIMIT 3;
Try it in the SQL Compiler

Result:

product_nameurl_slug
Redmi Note 13 ProRedmi-Note-13-Pro
boAt Airdopes 141boAt-Airdopes-141
Samsung 43" Smart TVSamsung-43"-Smart-TV

(3 rows)

Common mistakes

  • || with a NULL column. One NULL makes the whole result NULL. Use CONCAT or COALESCE.
  • Counting positions from 0. SQL strings start at position 1.
  • Comparing untrimmed text. 'UPI ' does not equal 'UPI'. Trim before you compare or group.

Practice question

For each customer, show their name and the username part of their email (before the @). Skip customers without an email.

Show solution
SELECT customer_name,
       SPLIT_PART(email, '@', 1) AS username
FROM customers
WHERE email IS NOT NULL
LIMIT 5;
Try it in the SQL Compiler

Result:

customer_nameusername
Aarav Sharmaaarav.sharma
Priya Nairpriya.nair
Rohan Guptarohan.g
Sneha Reddysneha.reddy
Vikram Singhvikram.singh

(5 rows)

Key takeaways

  • UPPER, LOWER and INITCAP standardise case.
  • || and CONCAT join text; CONCAT ignores NULLs.
  • SUBSTRING, LEFT, RIGHT and SPLIT_PART extract pieces.
  • TRIM and REPLACE clean messy values before analysis.

Frequently asked questions

How do I concatenate strings in SQL?

Use CONCAT(a, b) or the || operator in PostgreSQL. CONCAT treats NULL as an empty string, while a || NULL returns NULL. In SQL Server you can also use +.

Do SQL string positions start at 0 or 1?

At 1. SUBSTRING('ShopKart', 1, 4) returns 'Shop'.

How do I split a string in SQL?

In PostgreSQL use SPLIT_PART(text, delimiter, n). MySQL uses SUBSTRING_INDEX, and SQL Server uses STRING_SPLIT.

Newsletter

Keep learning SQL

One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.

Prefer a one-page summary? Download the free SQL cheat sheet (PDF)