|
1 | 1 | ---
|
2 |
| -title: SUBSTRING in Azure Cosmos DB query language |
3 |
| -description: Learn about SQL system function SUBSTRING in Azure Cosmos DB. |
4 |
| -author: ginamr |
| 2 | +title: SUBSTRING |
| 3 | +titleSuffix: Azure Cosmos DB for NoSQL |
| 4 | +description: An Azure Cosmos DB for NoSQL system function that returns a portion of a string using a starting position and length. |
| 5 | +author: jcodella |
| 6 | +ms.author: jacodel |
| 7 | +ms.reviewer: sidandrews |
5 | 8 | ms.service: cosmos-db
|
6 | 9 | ms.subservice: nosql
|
7 |
| -ms.topic: conceptual |
8 |
| -ms.date: 09/13/2019 |
9 |
| -ms.author: girobins |
10 |
| -ms.custom: query-reference, ignite-2022 |
| 10 | +ms.topic: reference |
| 11 | +ms.date: 07/24/2023 |
| 12 | +ms.custom: query-reference |
11 | 13 | ---
|
12 |
| -# SUBSTRING (Azure Cosmos DB) |
| 14 | + |
| 15 | +# SUBSTRING (NoSQL query) |
| 16 | + |
13 | 17 | [!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
|
14 | 18 |
|
15 |
| - Returns part of a string expression starting at the specified character zero-based position and continues to the specified length, or to the end of the string. |
16 |
| - |
| 19 | +Returns part of a string expression starting at the specified position and of the specified length, or to the end of the string. |
| 20 | + |
17 | 21 | ## Syntax
|
18 |
| - |
| 22 | + |
19 | 23 | ```sql
|
20 |
| -SUBSTRING(<str_expr>, <num_expr1>, <num_expr2>) |
21 |
| -``` |
22 |
| - |
| 24 | +SUBSTRING(<string_expr>, <numeric_expr_1>, <numeric_expr_2>) |
| 25 | +``` |
| 26 | + |
23 | 27 | ## Arguments
|
24 |
| - |
25 |
| -*str_expr* |
26 |
| - Is a string expression. |
27 |
| - |
28 |
| -*num_expr1* |
29 |
| - Is a numeric expression to denote the start character. A value of 0 is the first character of *str_expr*. |
30 |
| - |
31 |
| -*num_expr2* |
32 |
| - Is a numeric expression to denote the maximum number of characters of *str_expr* to be returned. A value of 0 or less results in empty string. |
| 28 | + |
| 29 | +| | Description | |
| 30 | +| --- | --- | |
| 31 | +| **`string_expr`** | A string expression. | |
| 32 | +| **`numeric_expr_1`** | A numeric expression to denote the start character. | |
| 33 | +| **`numeric_expr_2`** | A numeric expression to denote the maximum number of characters of `string_expr` to be returned. | |
33 | 34 |
|
34 | 35 | ## Return types
|
35 |
| - |
36 |
| - Returns a string expression. |
37 |
| - |
| 36 | + |
| 37 | +Returns a string expression. |
| 38 | + |
38 | 39 | ## Examples
|
39 |
| - |
40 |
| - The following example returns the substring of "abc" starting at 1 and for a length of 1 character. |
41 |
| - |
42 |
| -```sql |
43 |
| -SELECT SUBSTRING("abc", 1, 1) AS substring |
44 |
| -``` |
45 |
| - |
46 |
| - Here is the result set. |
47 |
| - |
48 |
| -```json |
49 |
| -[{"substring": "b"}] |
50 |
| -``` |
| 40 | + |
| 41 | +The following example returns substrings with various lengths and starting positions. |
| 42 | + |
| 43 | +:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/substring/query.sql" highlight="2-5"::: |
| 44 | + |
| 45 | +:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/substring/result.json"::: |
51 | 46 |
|
52 | 47 | ## Remarks
|
53 | 48 |
|
54 |
| -This system function will benefit from a [range index](../../index-policy.md#includeexclude-strategy) if the starting position is `0`. |
| 49 | +- This function benefits from a [range index](../../index-policy.md#includeexclude-strategy) if the starting position is `0`. |
| 50 | +- `numeric_expr_1` positions are zero-based, therefore a value of `0` starts from the first character of `string_expr`. |
| 51 | +- A value of `0` or less for `numeric_expr_2` results in empty string. |
55 | 52 |
|
56 | 53 | ## Next steps
|
57 | 54 |
|
58 | 55 | - [System functions Azure Cosmos DB](system-functions.yml)
|
59 |
| -- [Introduction to Azure Cosmos DB](../../introduction.md) |
| 56 | +- [`StringEquals`](stringequals.md) |
0 commit comments