sqlcmd.net validated sql reference
intermediate string-processing MySQL MariaDB SQL Server PostgreSQL SQLite

Extract Part Of A String With SUBSTRING

Return only part of a text value by position and length.

Docker-validated Not currently validation-green

Take the first three letters of a code

This example reads the first three characters and ignores the remainder of the source string.

Rows loaded before the example query runs.
Setup
CREATE TABLE products (sku VARCHAR(20));

INSERT INTO
  products (sku)
VALUES
  ('ABC-123');
Shared across supported engines.
SQL
SELECT
  SUBSTRING(sku, 1, 3) AS sku_prefix
FROM
  products;
Returned rows for the shared example.
sku_prefix
ABC

Each supported engine accepts the same `SUBSTRING` call used here.

Where this command helps.

  • extracting a code or prefix from a structured string
  • taking a fixed-width slice from imported text

What the command is doing.

SUBSTRING is useful when a column stores structured text and you only need one segment of it, such as a prefix, code, or fixed-width slice. It can also help when prototyping text parsing before moving the logic elsewhere.