SQL: How To Append Semi-Colon To Column Value. - mysql

I have a column in my database named details_location which references the second part of a url ex. "manager\reports\user\description\UsersRights.htm" I'd like to append a semi-colon to each row value, so that the above appears as "manager\reports\user\description\UsersRights.htm;"
I've tried using something like
update reportdescriptions
set details_location = details_location+';'
To no avail, I'm sure it's something simple I'm missing. Thanks!

Try CONCAT function
update reportdescriptions set details_location = concat(details_location,';')

Try this:
update reportdescriptions
set details_location = CONCAT(details_location, ';');
For more info on CONCAT, check THIS.

You want concat(field_name,"string to add")

What error you get?
If you have a column of type text you may need to cast it to varchar() first
or CONCAT() like
UPDATE reportdescriptions
SET details_location = CONCAT(details_location , ';');

Related

MySQL update to change all instances containing -html to .html

I need to update a bunch of records in my database that store a slug in an article table. I mistakenly set the slugs to end in "-html" rather than ".html" and I need a query that will fix this.
I don't really understand how to use variables, so I'm hoping someone here can help.
Would someone please write for me a SQL query that's something like:
UPDATE table
SET table.slug = '%.html%'
WHERE table.slug LIKE '%-html%'
Obviously, that's not correct, but I don't know the correct way to write it.
Here's a quick-and-dirty example using REPLACE()
UPDATE table
SET slug = REPLACE(slug, '-html', '.html')
WHERE slug LIKE '%-html'
Just be warned that this will replace any occurrence of -html, even if it's not at the end of the string.
A more comprehensive approach might be
UPDATE table
SET slug = CONCAT(TRIM(TRAILING '-html' FROM slug), '.html')
WHERE slug LIKE '%-html'
Mine will only replace the last '-html', and append '.html'by CONCAT:
UPDATE table
SET slug = CONCAT(SUBSTRING(slug, 1, LENGTH(slug) - 5), '.html')
WHERE slug LIKE '%-html'
You have to make use of replace command.
UPDATE Table Tablename
SET MyColumnname = REPLACE(MyColumnname, '-html', '.html')
WHERE MyColumnname LIKE '%-html%'
I'd make use the the TRIM and CONCAT functions:
Something like this:
UPDATE `table` t
SET t.slug = CONCAT(TRIM(TRAILING '-html' FROM t.slug),'.html')
WHERE t.slug LIKE '%-html'
Note that the TRIM(TRAILING '-html' will remove all occurrences of that specified string from the end of the column value, so if I had (for example) a column value of 'foo-html-bar-html-html', that would return 'foo-html-bar'.
I use the CONCAT function to append '.html'.
The WHERE clause guarantees that I will only be modifying rows that have a column value ending in '-html'.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html

Changing column content in many rows

I have a column called code and in every row, the column contains FE. Because I do not want to go through 13,000 records, is there a quick way to replace FE inside 'code' with FEU?
While I appreciate this may be a simple question (or not?), I wasn't sure how to word it in order to find a solution.
You should be able to do like this:
UPDATE <table> SET code = REPLACE(code, 'FE', 'FEU');
If the column really just contains the value FE a simple WHERE clause should be enough:
UPDATE <table> SET code = 'FEU' WHERE code = 'FE';
Maybe something like this?
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'
This will work if your column contains only the string 'FE' and you want to replace it with 'FEU'.
update `table`
set `code` = replace(`code` , "FE","FEU")
where (if there is a where write it here)
should do you
This is probably the quickest way. You can try this:-
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'

MySQL REPLACE variable string

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??
USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))
Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.
I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).
first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')
try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

MYSQL Concat not working

UPDATE profile SET
favourties=CONCAT(favourties,"123")
WHERE id=1
i want to append 123 in favourties but if default value of favourties is set to NULL then this query will not work. What will be query if favourties is set to NULL and then append 123 with it
UPDATE profile SET favourties=CONCAT(IFNULL(favourties, ''),"123") WHERE id=1
Wrap the field around with the COALESCE function:
UPDATE profile
SET favourties = CONCAT(COALESCE(favourties, ''),"123")
WHERE id=1
You probably can't concatenate something to NULL. Maybe you can use coalesce?
UPDATE profile SET favourties=CONCAT(COALESCE(favourites,""),"123") WHERE id=1
see: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
You can use ifnull like #zerkms says, but that is not in the SQL standard. It is a tiny tad faster though. Read up on it at this link: http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx
You can also use CONCAT_WS() (docs) which deals with NULLs as you would expect: converts them to an empty string:
UPDATE profile SET favourties = CONCAT_WS('', favourties, "123") WHERE id = 1;
I personally actually only use CONCAT_WS() now because having to worry about NULLs annoys me. I rarely use NULLs so I don't have to worry about it, but just incase. It's just an annoying thing to figure out why you you're ending up with an empty string when it just doesn't seem to make sense.
In PHP, I use:
SET `trans`=concat('$var', trans)
to add to a string already in the trans column.
It wouldn't work on column named group without using back ticked group inside the brackets as well, whereas with trans, back ticks were not needed.

Mysql Update Field Contents

I am currently trying to edit my db named boh. The current table "files" has a field called "path". Inside the path field is an actualpath to files listed in a folder, syntax "F:\xxx\xxx\xxx\filename.xxx". How do I update the field information to replace the "F:\xxx\xxx\xxx" so that just the file name exists?
It depends what you exactly want, if you want to strip constant path you can use:
UPDATE `table` SET `path` = REPLACE(`path`, 'F:\\xxx\\xxx\\xxx', '');
If you want to keep only last part after last \, then following command should do it:
UPDATE `table` SET `path` = SUBSTRING_INDEX(`path`. '\\', -1);
did you read this?
http://dev.mysql.com/doc/refman/5.1/en/replace.html
UPDATE files
SET path = REPLACE(path, 'F:\xxx\xxx\xxx\', '')
WHERE path LIKE = 'F:\xxx\xxx\xxx\%'
It's very easy to ruin your data with this massive updates so make sure you:
Try it first with a SELECT sentence
Backup your data
Assuming 'F:\xxx\xxx\xxx\' is not constant you could try a statement like this one:
UPDATE files SET path = REVERSE(SUBSTR(REVERSE(path), 1, LOCATE(REVERSE(path), '\')));