HomeIntegrationsPricingLearn
Sign inGet started

Data to decisions, faster.

Book a Demo

Learn

  • Blog
  • Webinars
  • SQL tutorials

Legal & security

  • Privacy Policy
  • Terms of Use
  • Cookies Policy
  • Trust Center
  • Security

© 2026 Count Technologies Ltd. All rights reserved.

SQL Resources

Count is the best SQL IDE, wrapped up in the best data analysis tool, all inside the best BI platform.

ARRAY_AGG
Arrays Explained
Arrays Explained
CASE
CAST
COALESCE
CONCAT
COUNT [DISTINCT]
CURRENT_DATE
CURRENT_DATETIME
CURRENT_TIMESTAMP
DATETIME_DIFF
DATETIME_TRUNC
DATE_DIFF
DATE_TRUNC
Data Types
Dates and Times
EXTRACT
IF
MEDIAN
SUBSTR
DefinitionPractical InfoCommon QuestionsTroubleshooting Common Errors
String Functions Explained
TIMESTAMP_DIFF
TIMESTAMP_TRUNC
TIME_DIFF
TIME_TRUNC
UNNEST
Window Functions Explained
SQL Resources/BigQuery Standard SQL/SUBSTR

SUBSTR

SUBSTR function. Definition, syntax, examples and common errors using BigQuery Standard SQL. The substring allows you to extract a section of a longer string in BigQuery.

Definition

The SUBSTR function allows you to extract a section of a longer string in BigQuery.

locationstate
New York, NYNY
Miami, FLFL
ℹ️

The query above gets the last 2 characters of the location string.

Practical Info

  • SUBSTR works with STRING or BYTES data types
  • The position argument is 1-based, so the first character is at position 1
  • You can use -1 to indicate the last character position
  • if length < 0 then the function returns an error

Common Questions

How to dynamically extract sub-strings?

SUBSTR(value, position[, length])

What if we have a series of email addresses: abc@gmail.com, xy@yahoo.com, etc. and we want to find the domain (e.g. gmail.com, yahoo.com), we can use SUBSTR in combination with STRPOS and LENGTH to dynamically extract everything after the @.

emaildomain
abc@gmail.comgmail.com
xy@yahoo.comyahoo.com

Alternatively, you can use REGEXP_EXTRACT to achieve the same result:

SELECT
  location,
  SUBSTR(location, (-2), 2) AS state
FROM
  (
    SELECT
      'New York, NY' AS location
    UNION ALL
(    SELECT
      'Miami, FL' AS location)
  ) AS table_2
emaildomain
abc@gmail.comgmail.com
xy@yahoo.comyahoo.com

Troubleshooting Common Errors

It's rare SUBSTR returns an error unless you call it with the wrong data types (e.g. pass a STRING to the position parameter), but it's often that you don't get what you want.

The best thing to do here is to either use REGEXP functions, or start get your query working with 1-2 example rows before trying to apply it to the entire column.

SELECT
  email,
  SUBSTR(email, (STRPOS(email, '@') + 1), (LENGTH(email) - STRPOS(email, '@'))) AS domain
FROM
  (
    SELECT
      'abc@gmail.com' AS email
    UNION ALL
(    SELECT
      'xy@yahoo.com' AS email)
  ) AS table_2
SELECT
  email,
  REGEXP_EXTRACT(email, r'@([a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)') AS domain
FROM
  (
    SELECT
      'abc@gmail.com' AS email
    UNION ALL
(    SELECT
      'xy@yahoo.com' AS email)
  ) AS table_2

Got a CSV?
See it differently in <2 mins

Get started for FREEWatch our CEO do it