---
schema_version: "1"
id: 26dbbed3a4e831df78f636e538947417083d70dbab88644be78307d61bbef04a
title: substring({str} FROM {start} FOR {length})
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_sql/guides/bi-sql-string-substring.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-sql/guide/bi-sql-string-substring.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: f291def2c0ae961085925de5db8cec9bb09c376f0403f594ec55bc2dab0cd71e
products:
  - SQL for Analytics Developer Guide
  - analytics
---

# substring({str} FROM {start} FOR {length})

Returns a substring from string _str_ that begins with the character at the _start_ position.

**note:** The substring function isn’t supported in Analytics Studio. An error occurs when the function is used in the UI. The function is available using the Query API. For more information, see [Query CRM Analytics Data with the Query API](https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_rest.meta/bi_dev_guide_rest/bi_rest_api_query_data.htm).

- `start`

  - : Required. The starting position of the substring. This is the first character of the substring. The value can be positive or negative. If positive, the position is taken from the beginning of the string. If negative, the position is taken from the end of the string.

- `length`

  - : Optional. The length of the substring, beginning at the _start_ position. If no value is provided, `substring()` extracts characters from the _start_ position through the end of the string.

## Example

This example extracts the first three letters from each City field.

```nolang
SELECT City, SUBSTRING(City FROM 1 FOR 3) as CityCode FROM "Superstore"
GROUP BY City LIMIT 5;

```

| City        | CityCode |
| ----------- | -------- |
| Aberdeen    | Abe      |
| Abilene     | Abi      |
| Akron       | Akr      |
| Albuquerque | Alb      |
| Alexandria  | Ale      |
