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
Copy file name to clipboardExpand all lines: content/sql/concepts/string-function/terms/substring/substring.md
+94-7Lines changed: 94 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
Title: 'SUBSTRING()'
3
-
Description: 'Extracts a part of a string.'
3
+
Description: 'Extracts a portion of a string starting from a specified position.'
4
4
Subjects:
5
5
- 'Data Science'
6
6
- 'Data Visualization'
@@ -16,28 +16,115 @@ CatalogContent:
16
16
17
17
In SQL, the **`SUBSTRING()`** function takes a slice from a string containing data in binary, character, text, or image format. Upon extraction, the [`SELECT`](https://www.codecademy.com/resources/docs/sql/commands/select) statement can be used to select and manipulate the extracted substring according to the [data type](https://www.codecademy.com/resources/docs/sql/data-types). The data type is the same as the original string except for the next expressions.
18
18
19
-
## Syntax
19
+
## SQL `SUBSTRING()`Syntax
20
20
21
21
```pseudo
22
22
SUBSTRING(string, start, length)
23
23
```
24
24
25
+
**Parameters:**
26
+
25
27
-`string`: The string to extract a substring from.
26
-
-`start`: The `string` index from which the substring starts. The minimum possible value is 1.
28
+
-`start`: The `string` index from which the substring starts. The minimum possible value is `1`.
27
29
-`length`: The number of characters to extract from `string`.
28
30
29
-
> Note: Not all services support this function. Oracle and SQLite use `SUBSTR()` to accomplish the same goal.
31
+
**Return value:**
32
+
33
+
Returns a substring of the original string. If `length` exceeds the remaining characters, the result contains everything from `start` to the end of the string.
34
+
35
+
> **Note:** Not all services support this function. Oracle and SQLite use `SUBSTR()` to accomplish the same goal.
30
36
31
-
## Example
37
+
## Example 1: Using `SUBSTRING()` on a String Literal
32
38
33
-
The below example shows how to use `SUBSTRING()`:
39
+
This query uses `SUBSTRING()` on a string literal:
34
40
35
41
```sql
36
42
SELECTSUBSTRING('Codecademy', 1, 4) AS ExtractString;
37
43
```
38
44
39
-
The code above generates the following output:
45
+
Here is the output:
40
46
41
47
| ExtractString |
42
48
| ------------- |
43
49
| Code |
50
+
51
+
## Example 2: Using `SUBSTRING()` on a Table Column
52
+
53
+
Suppose we have a table named `Employees`:
54
+
55
+
| EmployeeID | FullName |
56
+
| ---------- | ------------- |
57
+
| 1 | John Carter |
58
+
| 2 | Alice Johnson |
59
+
| 3 | Michael Brown |
60
+
61
+
This query uses `SUBSTRING()` on the `FullName` column of the `Employees` table:
62
+
63
+
```sql
64
+
SELECT FullName, SUBSTRING(FullName, 1, 4) AS FirstFourChars
65
+
FROM Employees;
66
+
```
67
+
68
+
Here is the output:
69
+
70
+
| FullName | FirstFourChars |
71
+
| ------------- | -------------- |
72
+
| John Carter | John |
73
+
| Alice Johnson | Alic |
74
+
| Michael Brown | Mich |
75
+
76
+
## Example 3: Extracting Domain from an Email Using `SUBSTRING()`
0 commit comments