Replace commas with single quotes - mysql

I have a parameter being passed to a stored procedure. The value of the parameter looks like this:
0001,0002,0003
I need for it to look like this:
'0001','0002','0003'
I've tried several ways of escaping the single quotes in the Replace function, but the MySQL Workbench keeps giving me syntax errors.
replace(AccountNumber, ',', '\',\'');
replace(AccountNumber, ',', '','');
I get an 'Unexpected' syntax error on the find text and an 'Unexpected' syntax error on the replace with text.
Any help would be appreciated.

You can do it with a query like this:
SELECT CONCAT( "'"
,REPLACE('0001,0002,0003',',',"','")
, "'");
sample
MariaDB [bernd]> SELECT CONCAT( "'",REPLACE('0001,0002,0003',',',"','"), "'");
+-------------------------------------------------------+
| CONCAT( "'",REPLACE('0001,0002,0003',',',"','"), "'") |
+-------------------------------------------------------+
| '0001','0002','0003' |
+-------------------------------------------------------+
1 row in set (0.02 sec)

Your first syntax is correct; something else in your statement that you don't show is wrong. Though you will also want to add single quotes around the whole thing:
select concat('\'',replace(AccountNumber, ',', '\',\''),'\'')
See fiddle.
You might find it more convenient to use double quotes:
select concat("'",replace(AccountNumber, ',', "','"),"'")

This data is coming from a SSRS report, which I do not have access to, otherwise I would modify it there.
My issue, as it turns out, was that I just had the Replace statement appearing on a line, I wasn't setting the returned value to a variable. So once I added
Set #AccntNo = CONCAT("'", Replace(....), "'");
The syntax errors went away. But #[Bernd Buffen] and #ysth had it right.

Related

CONCAT substring

I have a table which carries around 172 entries, with different column names, however I want to update all of them with a simple query.
I have a name entered in the name column (http://prntscr.com/j9qeg6)
I would like to replace the III with I've using a simple query,
Now I've been checking and trying however it does not seem to work.
I used the following query which got me closest to the result however its not working.
UPDATE item_template SET name = CONCAT("IV", SUBSTRING(name, LENGTH("III ")+1));
Does anyone have an idea on this?
Apostrophe ' instead of Double quotes "
You can try use REPLACE function.
UPDATE item_template
SET name = REPLACE(name, ' III', ' IV');
sqlfiddle:http://sqlfiddle.com/#!9/b4b8d6/1

Replace part of a column value in MySQL Select query

I have read the forums to find a solution for my issue but I am stuck with a MySQL error when I use the query.
I want to extract part of a field, everything that is after \\nt4\applications\prod\hde\atn\ in the FILE_NAME column
Here is the query:
SELECT FILE_NAME,
REPLACE (FILE_NAME,'\\nt4\applications\prod\hde\atn\','') as newfilename
from atn_documents
It always return me a
syntax error near ''\
It looks like the string to look into can not contains \ character??
Can anyone drive me?
Thanks
Cedric
Use SUBSTRING_INDEX:
SELECT
SUBSTRING_INDEX(FILE_NAME,
'\\nt4\\applications\\prod\\hde\\atn\\',
-1) AS path
FROM yourTable;
Demo
The above query is a verbatim implementation of your requirement, since it returns only what is after the path of interest. Also note that the immediate reason why your query does not even run is that you need to escape backslashes by doubling them up \\ if you want them as literals.
You have to escape the "\" character in the query. You can add additional "\" to escape it.
e.g.
SELECT FILE_NAME, REPLACE (FILE_NAME,'\\nt4\\applications\\prod\\hde\\atn\\','') as newfilename from atn_documents

How do I replace the ' (quotation) character using a MySQL statement

I'm trying to create and XML feed with the result set of a SQL query. One of the fields contains ' characters which is causing an error in the resulting XML file.
How do I replace the ' character. I've read posts that suggest something like
Replace(my_column,'''','')
But this throws up a syntax error
Probably you're looking for something like this:
UPDATE your_table
SET your_column = REPLACE(your_column, '\'', ''');
But then again, I wonder why it's necessary to do that in DB, and not in the output code.
It should work
select replace('abc''def','''','')
SQLFiddle example
Use ' in place of '.
This will work:
Replace(my_column,"'",''')

removing characters from field in MS Access database table

Using MS Access 2010.
I have a field in a table that contains windows path names surrounded by quotes, like this
"C:\My Documents\Photos\img1.jpg"
"C:\My Documents\Photos\products\gizmo.jpg"
"C:\My Documents\Photos\img5.jpg"
and so on.
I need to get rid of the quotes so the column looks like this:
C:\My Documents\Photos\img1.jpg
C:\My Documents\Photos\products\gizmo.jpg
C:\My Documents\Photos\img5.jpg
Is there a way to write an update query to do this?
OR a better way to do it altogether?
If you will be doing this from within an Access session, using Access 2000 or later, you can use the Replace() function in an update query to remove the quotes. Remove would mean replace them with an empty string.
UPDATE YourTable
SET path_field = Replace(path_field, '"', '');
If any of those path strings could include quotes within them (yuck!), consider the Mid() function ... ask it to start at the 2nd character (skipping the lead quote), and return the number of characters equivalent to Len(path_field) - 2
UPDATE YourTable
SET path_field = Mid(path_field, 2, Len(path_field) - 2);
Either way, you may want to include a WHERE clause to ignore rows without path_field values.
WHERE Len(path_field) > 0
And if you must do this again when new data is added, use a different WHERE clause to ensure you UPDATE only those rows whose path_field values start and end with quotes.
WHERE path_field Like '"*"'
That was using the * wild card for Access' default ANSI 89 mode. If you will do this from ADO (ANSI 92 mode), use the % wild card.
WHERE path_field Like '"%"'
... or use ALike and the % wild card with either mode.
WHERE path_field ALike '"%"'
The solution with REPLACE already mentioned by others works, but removes ALL quotes, even if they are in the middle of the string.
If you only want to remove quotes at the beginning or at the end, but leave quotes in the middle of the string as they are, you can do it with the following two queries:
Remove first character if it's a quote:
update YourTable
set YourField = right(YourField, len(YourField) - 1)
where left(YourField, 1) = '"'
Remove last character if it's a quote:
update YourTable
set YourTable = left(YourField, len(YourField) - 1)
where right(YourField, 1) = '"'
To make this a permanent change, you might run an update query that looked something like this:
UPDATE [Your Table]
SET [Your Table].[Your Field] = Replace([Your Table].[Your Field],"""","")
This will get rid of all quotes, even if they aren't at the beginning or end. Post back if that's not exactly what you want.
Assuming your column name is MyColumn and table name is MyTable, you can use this sql to update your data to get rid of quotes.
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'"','')

mysql - replace ' with single quote in a field

I'm not exactly sure what happened because I migrated from One server to another of the same spec and SQL...
Still in comments and titles the new database shows the characters ' instead of '
and I was wondering if I could ask for help in either replacing ' with '
or if it was simpler just deleting '
Thanks so much...
Steff
You could use MySQL's REPLACE method (look here):
UPDATE
Changed the statement to reflect the OP's naming:
UPDATE database1.vb_ppgal_albums
SET pp_photos = REPLACE(pp_photos, ''', '\'')
Good luck.
The following is the coding that I use to update double-quote in MySQL. I use the REPLACE function. The first parameter is the field_name that I want to have searched, the second is the escaping of the double quote (\") as the search string, followed by escaping of the escape character (\) followed by the double quote, which will insert into the field name (\"). In the table I will now have a measurement of '1/2\"' instead of '1/2"', which was my goal. I hope this helps. (PS, the Where clause is for show, you may not need it.)
UPDATE `table_name`
SET
`field_name` = REPLACE(`field_name`, '\"', '\\"')
WHERE `Id` > 125