Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Remove part of the string from all the entries returned by the SELECT statement

The following is the select statement that returns all the URL's containing a %20 (space) at the end of it:

select * from Table1 where Column1 like '%%20'

How can the '%20' be removed from all these URLs using SQL?

Edit 1:

Is there a way to remove '%20' only if it is found in the end of the URL (saving all other instances on '%20' in the URL)?

like image 906
Yash Saraiya Avatar asked Mar 09 '17 07:03

Yash Saraiya


People also ask

How do I remove a specific part of a string in SQL?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.

How do I select only part of a string in SQL?

The SUBSTRING() function extracts some characters from a string.

How can I replace part of a string in SQL?

The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.

How can I remove last 5 characters from a string in SQL?

Below is the syntax for the SUBSTRING() function to delete the last N characters from the field. Syntax: SELECT SUBSTRING(column_name,1,length(column_name)-N) FROM table_name; Example: Delete the last 2 characters from the FIRSTNAME column from the geeksforgeeks table.


1 Answers

This is simple. I assume that you are using Microsoft SQL Server. Try the following:

 Select SUBSTRING(Column1,  0, Len(Column1) - 2) As Column1, Column2 
 From Table1 where Column1 like '%[%]20'

In above, I've selected Column1 and %20 will be removed from the end of Column1 and selected Column2 as well. You can select all your columns.

like image 62
Muhammad Qasim Avatar answered Oct 22 '22 20:10

Muhammad Qasim