You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Syntax for [!INCLUDE [sssql25-md](../../includes/sssql25-md.md)] and later versions, [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)], [!INCLUDE [ssazuresynapse-md](../../includes/ssazuresynapse-md.md)], [!INCLUDE [ssazurepdw_md](../../includes/ssazurepdw_md.md)], and [!INCLUDE [fabric-dw-short](../../includes/fabric-dw-short.md)] and [!INCLUDE [fabric-se-short](../../includes/fabric-se-short.md)] in [!INCLUDE [fabric](../../includes/fabric.md)].
40
51
41
52
```syntaxsql
42
53
SUBSTRING ( expression , start [ , length ] )
43
54
```
44
55
56
+
> [!NOTE]
57
+
> In [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and earlier versions, [*length* is required](?view=sql-server-ver16&preserve-view=true#syntax).
58
+
59
+
::: moniker-end
60
+
45
61
## Arguments
46
62
47
63
#### *expression*
@@ -60,7 +76,7 @@ You can use substring with an optional *length* argument. However, if you use `N
60
76
61
77
## Return types
62
78
63
-
Returns character data if *expression* is one of the supported character data types. Returns binary data if *expression* is one of the supported **binary** data types. The returned string is the same type as the specified expression with the exceptions shown in the table.
79
+
Returns character data if *expression* is one of the supported character data types. Returns binary data if *expression* is one of the supported **binary** data types. The returned string is the same type as the specified expression with the exceptions shown in the following table.
64
80
65
81
| Specified expression | Return type |
66
82
| --- | --- |
@@ -70,7 +86,7 @@ Returns character data if *expression* is one of the supported character data ty
70
86
71
87
## Remarks
72
88
73
-
The values for *start* and *length*must be specified in number of characters for **ntext**, **char**, or **varchar** data types and bytes for **text**, **image**, **binary**, or **varbinary** data types.
89
+
You must specify the values for *start* and *length* in number of characters for **ntext**, **char**, or **varchar** data types. For **text**, **image**, **binary**, or **varbinary** data types, you must specify these values in bytes.
74
90
75
91
The *expression* must be **varchar(max)** or **varbinary(max)** when the *start* or *length* contains a value larger than 2,147,483,647.
76
92
@@ -120,7 +136,7 @@ bcd
120
136
> [!NOTE]
121
137
> To run the following examples, you must install the [**pubs** database](https://github.com/microsoft/sql-server-samples/tree/master/samples/databases/northwind-pubs).
122
138
123
-
The following example shows how to return the first 10 characters from each of a **text** and **image** data column in the `pub_info` table of the `pubs` database. **text** data is returned as **varchar**, and **image** data is returned as **varbinary**.
139
+
The following example shows how to return the first 10 characters from each of a **text** and **image** data column in the `pub_info` table of the `pubs` database. The query returns **text** data as **varchar**, and **image** data as **varbinary**.
124
140
125
141
```sql
126
142
USE pubs;
@@ -246,9 +262,9 @@ NULL
246
262
247
263
### E. Use SUBSTRING with optional length argument
248
264
249
-
**Applies to:**[!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)], [!INCLUDE [ssazuresynapse-md](../../includes/ssazuresynapse-md.md)], [!INCLUDE [ssazurepdw_md](../../includes/ssazurepdw_md.md)], and [!INCLUDE [fabric-se-short](../../includes/fabric-se-short.md)] and [!INCLUDE [fabric-dw](../../includes/fabric-dw.md)]
The following example shows how to return only a part of a character string from a given start position. Since the *length* argument isn't provided, the length defaults to return the remaining characters in the string.
267
+
The following example shows how to return only a part of a character string from a given start position. Since the *length* argument isn't provided, the function returns the remaining characters in the string.
252
268
253
269
```sql
254
270
SELECTSUBSTRING('123abc', 4) AS y;
@@ -265,7 +281,7 @@ abc
265
281
### F. Use SUBSTRING without a length argument to find replacement parts in AdventureWorks2022 inventory
0 commit comments