---
schema_version: "1"
id: e337e1a6838f586a3d4ebdc5263a8afb9e00589de472d4601e5f3f2e1a664d27
title: substr()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_saql/guides/reference/bi-saql-functions-string-substr.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-functions-string-substr.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3936d09efbbe22ef1c69e6d1276f857bfafbe0795a15b4a4498e1781dfe93133
products:
  - Analytics SAQL Developer Guide
  - analytics
---

# substr()

Returns a substring that starts at the specified position. You can also specify the length of the substring to return.

## Syntax

``substr`(string,position[, length])`

## Usage

substr returns the characters in _string_, starting at position _position_. If you specify _length_, this function returns _length_ number of characters. If any of the parameters are _null_, then the function returns _null_. _length_ is optional.

The first character in _string_ is at position 1. If _position_ is negative then the position is relative to the end of the string. So a _position_ of -1 denotes the last character.

If _length_ is negative, then the function returns _null_. If _position_ > len (_string_) or _position_ \< -len(_string_) or _position_ = 0, then the empty string is returned.

## Example

```nolang
-- we want a substring that is one character long, starting at position 1.
-- The character "C" is returned.
substr("CRM", 1, 1)

-- we want a substring that is 2 characters long, starting at position 1
-- The string "CR" is returned
substr("CRM", 1, 2) == "CR"

-- we want a substring that is two characters long, starting from the *end* of the string
-- The string "RM" is returned
substr("CRM", -2, 2) == "RM"

-- we want to get the first 10 characters from this string
-- the string "2018-03-16" is returned
substr("2018-03-16T00:00:03.000Z",10)
```

## Example

Suppose that you want to display the current time, but not the current date. Use `substr()` to return the last 11 characters from `date_to_string()` .

```nolang
q = foreach q generate substr(date_to_string(now(), "yyyy-MM-dd HH:mm:ss"), -11) as 'Time Now';
```
