SQL statement to remove part of a string [duplicate]

Possible Duplicate:
str_replace in SQL UPDATE?
How to remove part of string in mysql?
SQL Find & Replace part of a string

I have a database table with a list of website urls e.g. http://website.com/ and I want to remove all the http:// and https:// from them. Is their a simple SQL statement I could run on a column to remove it?

I've had a search around, but I can't find what I need. I'm presuming I need to use both REPLACE and UPDATE but I'm struggling.

So far I have:

UPDATE list
SET    website
WHERE  website LIKE 'http://%';

Is that correct? I'm using MySQL and the table is list, and column is website and I want to remove the http:// so a url like: http://website.com/ becomes just: website.com

EDIT: Is it also possible to remove a trailing slash as well?

Asked By: Cameron
||

Answer #1:

Have a look at the REPLACE function. You'll need to use it twice to remove both http and https.

UPDATE    list
SET       website = REPLACE(REPLACE(website, 'https://', ''), 'http://', '')
WHERE     website like 'https://%' 
  OR      website like 'http://%'

To handle trailing slashes, you can use the RIGHT, LEFT, and LENGTH functions.

UPDATE    list
SET       website = LEFT(website, LENGTH(website) - 1)
WHERE     RIGHT(website, 1) = '/'

Here is some documentation that you may find useful: MySQL string functions

Answered By: Jeremy Wiggins

Answer #2:

Why not just replace the string if it exists:

UPDATE list
SET    website = Replace(website, 'http://', '')
Answered By: Bridge

Answer #3:

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’) where CONDITION
Answered By: Shehzad Bilal
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles