Skip to content

Commit 8efc2ce

Browse files
authored
Correction to the initial article on "How to Remove Leading Zero" (#205)
* create-altering-student-table-MySQL-PostgreSQL * Add files via upload * Update altering-student-table-MySQL-PostgreSQL * Update altering-student-table-SQL-Server.sql * Update using-TRIM-with-SQL-Server.sql * Rename using-TRIM-function-with-MySQL.sql to trimming-with-MySQL.sql * Rename using-TRIM-with-PostgreSQL.sql to trimming-with-PostgreSQL.sql * Rename using-TRIM-with-SQL-Server.sql to trimming-with-SQL-Server.sql * Rename altering-student-table-MySQL-PostgreSQL to altering-student-table-MySQL-PostgreSQL.sql
1 parent 8b99e84 commit 8efc2ce

12 files changed

+73
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ALTER TABLE Student ADD COLUMN formatted_id VARCHAR(10);
2+
3+
UPDATE Student
4+
SET formatted_id = CONCAT('0', id)
5+
WHERE id <= 2000;
6+
7+
UPDATE Student
8+
SET formatted_id = id
9+
WHERE formatted_id IS NULL;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Add a new column to the Student table
2+
ALTER TABLE Student ADD formatted_id VARCHAR(10);
3+
4+
-- Update the new column with leading zeros
5+
UPDATE Student
6+
SET formatted_id = '0' + CAST(id AS VARCHAR(10))
7+
WHERE id <= 2000;
8+
9+
-- Update remaining rows where formatted_id is NULL
10+
UPDATE Student
11+
SET formatted_id = CAST(id AS VARCHAR(10))
12+
WHERE formatted_id IS NULL;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT name, formatted_id
2+
FROM Student
3+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT name, formatted_id,
2+
TRIM(LEADING '0' FROM formatted_id) AS cleaned_id
3+
FROM Student
4+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT name, formatted_id,
2+
LTRIM(formatted_id, '0') AS cleaned_id
3+
FROM Student
4+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT name, formatted_id,
2+
SUBSTRING(formatted_id,
3+
PATINDEX('%[^0]%', formatted_id),
4+
LEN(formatted_id)
5+
) AS cleaned_id
6+
FROM Student
7+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT name, formatted_id,
2+
CAST(formatted_id AS UNSIGNED) AS cleaned_id
3+
FROM Student
4+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
name,
3+
formatted_id,
4+
CAST(formatted_id AS INTEGER) AS cleaned_id
5+
FROM Student
6+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
name,
3+
formatted_id,
4+
CAST(formatted_id AS INT) AS cleaned_id
5+
FROM Student
6+
WHERE id in (1607,1007,2017,1010,1111,2008);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
name,
3+
formatted_id,
4+
CONVERT(formatted_id, UNSIGNED) AS cleaned_id
5+
FROM Student
6+
WHERE id IN (1607,1007,2017,1010,1111,2008);

0 commit comments

Comments
 (0)