☰ Learn SQL Tutorial Menu

String Functions

Written by CSA mentors · Updated 16 Sept 2026 · 3 min read


Real-world text data is messy — inconsistent casing, extra whitespace, names that need combining. String functions clean and transform text directly in SQL.

Concatenation

-- ANSI standard (works in PostgreSQL, SQLite, SQL Server 2012+)
SELECT first_name || ' ' || last_name AS full_name FROM employees;

-- MySQL (also works in SQL Server)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;

Changing case

SELECT UPPER(first_name), LOWER(last_name) FROM employees;

Trimming whitespace

SELECT TRIM('  padded value  ') AS cleaned;
SELECT LTRIM('  left padded');
SELECT RTRIM('right padded  ');

Length

SELECT product_name, LENGTH(product_name) AS name_length FROM products;

SQL Server uses LEN() instead of LENGTH().

Substrings

-- First 3 characters
SELECT SUBSTRING(product_name, 1, 3) FROM products;

SQLite uses SUBSTR(); the rest generally accept both SUBSTR and SUBSTRING.

Replacing text

SELECT REPLACE(email, '@csa-demo.local', '@company.com') FROM employees;

Finding position

-- Position of '@' within the email (1-based)
SELECT email, INSTR(email, '@') FROM employees; -- SQLite / MySQL
SELECT email, POSITION('@' IN email) FROM employees; -- PostgreSQL
SELECT email, CHARINDEX('@', email) FROM employees; -- SQL Server

Putting it together

SELECT
  UPPER(TRIM(first_name)) || ' ' || UPPER(TRIM(last_name)) AS display_name,
  LENGTH(first_name) AS first_name_length
FROM employees
WHERE first_name LIKE 'A%';

Open the Code Playground, pick any SQL engine (SQLite, PostgreSQL, MySQL, or SQL Server), and paste these queries in directly — the practice database above is already loaded for you, every time.

Lesson 15 of 27

Sign in to track your progress and earn learning points for every lesson you finish.

Example

-- A ready-to-query practice database is loaded automatically before every run —
-- no CREATE TABLE needed. Available tables:
--   departments  (id, name, location)
--   employees    (id, first_name, last_name, email, department_id, job_title, salary, hire_date, manager_id)
--   customers    (id, customer_name, city, country, signup_date)
--   products     (id, product_name, category, price, stock_quantity)
--   orders       (id, customer_id, employee_id, order_date, status)
--   order_items  (id, order_id, product_id, quantity, unit_price)

SELECT * FROM employees WHERE department_id = 1;
Try it Yourself »