Skip to content

Commit 559862d

Browse files
authored
[Edit] SQL: SUBSTRING() (#7473)
* [Edit] SQL: SUBSTRING() * updated an faq and some content tweaks to add keywords ---------
1 parent e021ac9 commit 559862d

File tree

1 file changed

+94
-7
lines changed
  • content/sql/concepts/string-function/terms/substring

1 file changed

+94
-7
lines changed

content/sql/concepts/string-function/terms/substring/substring.md

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: 'SUBSTRING()'
3-
Description: 'Extracts a part of a string.'
3+
Description: 'Extracts a portion of a string starting from a specified position.'
44
Subjects:
55
- 'Data Science'
66
- 'Data Visualization'
@@ -16,28 +16,115 @@ CatalogContent:
1616

1717
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.
1818

19-
## Syntax
19+
## SQL `SUBSTRING()` Syntax
2020

2121
```pseudo
2222
SUBSTRING(string, start, length)
2323
```
2424

25+
**Parameters:**
26+
2527
- `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`.
2729
- `length`: The number of characters to extract from `string`.
2830

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.
3036
31-
## Example
37+
## Example 1: Using `SUBSTRING()` on a String Literal
3238

33-
The below example shows how to use `SUBSTRING()`:
39+
This query uses `SUBSTRING()` on a string literal:
3440

3541
```sql
3642
SELECT SUBSTRING('Codecademy', 1, 4) AS ExtractString;
3743
```
3844

39-
The code above generates the following output:
45+
Here is the output:
4046

4147
| ExtractString |
4248
| ------------- |
4349
| 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()`
77+
78+
Suppose we have a table `Users`:
79+
80+
| UserID | Email |
81+
| ------ | ----------------- |
82+
83+
84+
85+
86+
This query uses `SUBSTRING()` to extract the domains from the emails in the `Email` column of the `Users` table:
87+
88+
```sql
89+
SELECT Email, SUBSTRING(Email, CHARINDEX('@', Email) + 1, LEN(Email)) AS Domain
90+
FROM Users;
91+
```
92+
93+
Here is the output:
94+
95+
| Email | Domain |
96+
| ----------------- | ----------- |
97+
| [email protected] | example.com |
98+
| [email protected] | company.org |
99+
| [email protected] | mysite.net |
100+
101+
## Frequently Asked Questions
102+
103+
### 1. What is `SUBSTRING()` in SQL?
104+
105+
`SUBSTRING()` returns part of a string, starting at a specified character position, for a given length.
106+
107+
### 2. How do `SUBSTRING()` and `SUBSTR()` differ?
108+
109+
- `SUBSTRING()`: Standard SQL function, supported by many databases.
110+
- `SUBSTR()`: Common in Oracle and some versions of MySQL, with similar functionality but slightly different parameter handling.
111+
112+
### 3. How to extract a substring from a string in SQL?
113+
114+
In MySQL, you can use either `SUBSTRING()` or `SUBSTR()`, they work the same way:
115+
116+
```sql
117+
SELECT SUBSTRING('Hello World', 7, 5) AS Result;
118+
```
119+
120+
Or,
121+
122+
```sql
123+
SELECT SUBSTR('Hello World', 7, 5) AS Result;
124+
```
125+
126+
Here is the output:
127+
128+
| Result |
129+
| ------ |
130+
| World |

0 commit comments

Comments
 (0)