From 3ac5f045fedc0a9b10afcbd7924bb8ce48d1dbb3 Mon Sep 17 00:00:00 2001 From: mauricemaina Date: Mon, 21 Oct 2024 06:43:26 +0300 Subject: [PATCH] remove-the-last-two-characters-from-a-string-column --- ...-characters-from-a-string-column-mysql.sql | 19 +++++++++++++++++++ ...acters-from-a-string-column-postgresql.sql | 13 +++++++++++++ ...racters-from-a-string-column-sqlserver.sql | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-mysql.sql create mode 100644 sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-postgresql.sql create mode 100644 sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-sqlserver.sql diff --git a/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-mysql.sql b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-mysql.sql new file mode 100644 index 00000000..624f4adb --- /dev/null +++ b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-mysql.sql @@ -0,0 +1,19 @@ +-- Using LEFT Function + +SELECT + LEFT(column_name, LENGTH(column_name) - 2) AS modified_name +FROM table_name; + +-- Using SUBSTRING Function + +SELECT + SUBSTRING(column_name, 1, LENGTH(column_name) - 2) AS modified_name +FROM table_name; + +-- Handling Edge Cases + +SELECT CASE + WHEN LENGTH(column_name) > 2 THEN LEFT(column_name, LENGTH(column_name) - 2) + ELSE column_name +END AS modified_name +FROM table_name; diff --git a/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-postgresql.sql b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-postgresql.sql new file mode 100644 index 00000000..eb604bf2 --- /dev/null +++ b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-postgresql.sql @@ -0,0 +1,13 @@ +-- Using SUBSTRING Function + +SELECT + SUBSTRING(column_name FROM 1 FOR LENGTH(column_name) - 2) AS modified_name +FROM table_name; + +-- Handling Edge Cases + +SELECT CASE + WHEN LENGTH(column_name) > 2 THEN LEFT(column_name, LENGTH(column_name) - 2) + ELSE column_name +END AS modified_name +FROM table_name; diff --git a/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-sqlserver.sql b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-sqlserver.sql new file mode 100644 index 00000000..a9dcce2d --- /dev/null +++ b/sql-queries-7/remove-the-last-two-characters-from-a-string-column/remove-the-last-two-characters-from-a-string-column-sqlserver.sql @@ -0,0 +1,19 @@ +-- Using SUBSTRING Function + +SELECT + SUBSTRING(column_name, 1, LEN(column_name) - 2) AS modified_name +FROM table_name; + +-- Using LEFT Function + +SELECT + LEFT(column_name, LEN(column_name) - 2) AS modified_name +FROM table_name; + +-- Handling edge cases + +SELECT CASE + WHEN LEN(column_name) > 2 THEN LEFT(column_name, LEN(column_name) - 2) + ELSE column_name +END AS modified_name +FROM table_name;