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,"'",''')
Related
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.
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
I am having the following problem:
I have a table T which has a column Name with names. The names have the following structure:
A\\B\C
You can create on yourself like this:
create table T ( Name varchar(10));
insert into T values ('A\\\\B\\C');
select * from T;
Now if I do this:
select Name from T where Name = 'A\\B\C';
That doesn't work, I need to escape the \ (backslash):
select Name from T where Name = 'A\\\\B\\C';
Fine.
But how do I do this automatically to a string Name?
Something like the following won't do it:
select replace('A\\B\C', '\\', '\\\\');
I get: A\\\BC
Any suggestions?
Many thanks in advance.
You have to use "verbatim string".After using that string your Replace function will
look like this
Replace(#"\", #"\\")
I hope it will help for you.
The literal A\\B\C must be coded as A\\\\A\\C, and the parameters of replace() need escaping too:
select 'A\\\\B\\C', replace('A\\\\B\\C', '\\', '\\\\');
output (see this running on SQLFiddle):
A\\B\C A\\\\B\\C
So there is little point in using replace. These two statements are equivalent:
select Name from T where Name = replace('A\\\\B\\C', '\\', '\\\\');
select Name from T where Name = 'A\\\\B\\C';
Usage of regular expression will solve your problem.
This below query will solve the given example.
1) S\\D\B
select * from T where Name REGEXP '[A-Z]\\\\\\\\[A-Z]\\\\[A-Z]$';
if incase the given example might have more then one char
2) D\\B\ACCC
select * from T where Name REGEXP '[A-Z]{1,5}\\\\\\\\[A-Z]{1,5}\\\\[A-Z]{1,5}$';
note: i have used 5 as the max occurrence of char considering the field size is 10 as its mentioned in the create table query.
We can still generalize it.If this still has not met your expectation feel free to ask for my help.
You're confusing what's IN the database with how you represent that data in SQL statements. When a string in the database contains a special character like \, you have to type \\ to represent that character, because \ is a special character in SQL syntax. You have to do this in INSERT statements, but you also have to do it in the parameters to the REPLACE function. There are never actually any double slashes in the data, they're just part of the UI.
Why do you think you need to double the slashes in the SQL expression? If you're typing queries, you should just double the slashes in your command line. If you're generating the query in a programming language, the best solution is to use prepared statements; the API will take care of proper encoding (prepared statements usually use a binary interface, which deals with the raw data). If, for some reason, you need to perform queries by constructing strings, the language should hopefully provide a function to escape the string. For instance, in PHP you would use mysqli_real_escape_string.
But you can't do it by SQL itself -- if you try to feed the non-escaped string to SQL, data is lost and it can't reconstruct it.
You could use LIKE:
SELECT NAME FROM T WHERE NAME LIKE '%\\\\%';
Not exactly sure by what you mean but, this should work.
select replace('A\\B\C', '\', '\\');
It's basically going to replace \ whereever encountered with \\ :)
Is this what you wanted?
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
I found a CSV database of Cities/ZIP/GPS, and when I imported it, it added a " infront of the columns.
alt text http://www.grabup.com/uploads/58754a865eebd94c9aafaf7444b52d15.png?direct
I don't want to go in for 33,000 entries and do this manually, is there a query I can run that will remove the quotes?
i'm not a MySql expert but this should work: (based on my similar experience in Sql Server)
UPDATE table_name SET col_name = REPLACE(col_name, '"', '')
For more info on the REPLACE and other string parsing functions, see here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
in sql server you coud do:
update mytable set state= substring(state,2,29)
change the "29" to whatever the actual length is.
I am sure mysql must have equivalent syntax.
Repeat for each field, it looks like there is only a handful of them.
As an alternative you could filter the original csv document - isn't that easier?