Louisa
Louisa

Reputation: 550

MySQL, adding a string to all column values

Have a MySQL database table with 2 columns: id, url

The url column has values like 'http://www.example.com/'.

I need to add a string like 'http://www.x.com?redirect=' to the beginning of all the column values - i.e. to change the values like this: http://www.example.com/ ===> http://www.x.com?redirect=http://www.example.com/

Anyone has some clues to how I can do that?

I have looked into using CONCAT(), but so far I haven't been able to make it work :(

Thanks a lot for your help, Louisa

Upvotes: 2

Views: 6847

Answers (4)

Eric
Eric

Reputation: 705

You can do this:

Update myTable
SET data= (SELECT CASE WHEN data IS NULL THEN '' ELSE data END AS data WHERE id = 1) + 'some text'
WHERE id = 1

field = field + value does not work when field is null.

Upvotes: 1

JW8
JW8

Reputation: 1506

Take a look at code snippet from this SO answer:

update t set data=concat(data, 'a');

Something similar should work:

update t set data=concat('http://www.x.com?redirect=', data);

Upvotes: 0

FeRtoll
FeRtoll

Reputation: 1267

Using concat it would be like this:

update table set url=concat('http://www.x.com?redirect=',url);

Upvotes: 7

Mark Byers
Mark Byers

Reputation: 838056

Yes, you use can use CONCAT:

SELECT CONCAT('http://www.x.com?redirect=', url) AS url
FROM yourtable

See it working online: sqlfiddle

Upvotes: 6

Related Questions