Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting part of a string in MYSQL

Tags:

string

sql

mysql

I want to delete part of a string found in a particular field.

For example, the entry in the field could be "01365320APS". The "APS" is what I am looking at deleting.

My question is, should I use:

SELECT SUBSTRING_INDEX('fieldname','APS', 1) 
like image 705
Mike Jones Avatar asked Jul 13 '11 16:07

Mike Jones


People also ask

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

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.

How do you delete part of a string?

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 remove a word from a string in MySQL?

Use the MySQL REPLACE() function to replace a substring (i.e. words, a character, etc.) with another substring and return the changed string. This function takes three arguments: The string to change.

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

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.


2 Answers

When you want to edit a field, you need an UPDATE statement:

UPDATE table SET fieldname=REPLACE(fieldname,'APS','') 

REPLACE is a string function that replaces every occurence of the 2nd string in the 1st string with the 3rd one.

Please try this with a WHERE clause first, to see if it is really what you want to do.

like image 155
Jacob Avatar answered Sep 21 '22 16:09

Jacob


For every occurrence of APS, try this:

UPDATE table SET column=REPLACE(column,'APS',''); 

Reference: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace

like image 37
AJ. Avatar answered Sep 19 '22 16:09

AJ.