Stop 04 · Functions & ExpressionsTopics · 1/8

String Functions (CONCAT, SUBSTRING, LENGTH, UPPER)

Part of the SQL Roadmap

In short

Names, codes, emails and addresses are all text, and reports constantly need them joined, cut or reshaped. String functions let you do that right inside the query.

String functions build and slice text: CONCAT joins values, SUBSTRING extracts part of a string, LENGTH counts characters and UPPER or LOWER change case. Names vary a little by database (LENGTH vs LEN, || for concatenation in PostgreSQL).

The jaadu sits at a tailor's table snipping a strip of bright cloth with big scissors, a measuring tape draped around its neck.

Think of it like this

The tailor's counter

A darzi stitches two pieces of cloth together (CONCAT), cuts out exactly the piece he needs (SUBSTRING), measures length with a tape (LENGTH) and can dye the cloth a bolder shade (UPPER). The original bolt of cloth on the shelf is never touched.

Watch it happen

Working on 'Priya Sharma'

P
0
r
1
i
2
y
3
a
4
5
S
6
h
7
a
8
r
9
m
10
a
11

1/5A text value is a row of characters. Positions in SQL start at 1, not 0.

The big ideas

What you need to know

CONCAT and ||

CONCAT(first_name, ' ', last_name) joins text. PostgreSQL also has ||. SQL Server uses + or CONCAT.

SUBSTRING cuts

SUBSTRING(text, start, length) with positions from 1. LEFT(text, n) and RIGHT(text, n) are handy shortcuts.

LENGTH counts

LENGTH(name) in PostgreSQL and MySQL. SQL Server calls it LEN. MySQL's LENGTH counts bytes, so use CHAR_LENGTH for Hindi text.

UPPER and LOWER

Change case for display, or for case-insensitive matching: WHERE LOWER(email) = 'a@x.in'.

See it in code

Walk through the code

sql
1SELECT2  CONCAT(first_name, ' ', last_name)     AS full_name,3  first_name || ' ' || last_name         AS full_name_pg,4  UPPER(LEFT(first_name, 1))             AS initial,5  SUBSTRING(phone, 1, 5)                 AS phone_prefix,6  LENGTH(first_name)                     AS name_length,7  SUBSTRING(email FROM POSITION('@' IN email) + 1) AS email_domain8FROM customers;

1/4Two ways to build a full name. CONCAT treats NULL as empty; || returns NULL if any part is NULL.

All lines run in PostgreSQL. MySQL supports every line except || (it means OR there). SQL Server uses LEN, CHARINDEX and + instead.

Save yourself the pain

Common mistakes

Oops

Using first_name || ' ' || last_name when last_name can be NULL.

Do this instead

The whole result becomes NULL. Use CONCAT, or CONCAT_WS(' ', first_name, last_name), which skips NULLs.

Oops

Assuming string positions start at 0, like in Python.

Do this instead

SQL positions start at 1. SUBSTRING(name, 0, 3) gives only two characters in PostgreSQL.

Your mission

Reshape names and emails

~20 min

Quick check

What does SUBSTRING('Bengaluru', 1, 4) return?

Go deeper

  1. 1Build a full_name column from first and last names.
  2. 2Extract the domain part of an email address.
  3. 3Check how CONCAT treats NULL in your database (MySQL CONCAT returns NULL, PostgreSQL CONCAT ignores it).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge