Replace part of a column value in MySQL Select query - mysql

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

Related

SQL - match last two characters in a string

I have a small mysql database with a column which has format of a field as following:
x_1_1,
x_1_2,
x_1_2,
x_2_1,
x_2_12,
x_3_1,
x_3_2,
x_3_11,
I want to extra the data where it matches last '_1'. So if I run a query on above sample dataset, it would return
x_1_1,
x_2_1,
x_3_1,
This should not return x_2_12 or x_3_11.
I tried like '%_1' but it returns x_2_12 and x_3_11 as well.
Thank you!
A simple method is the right() function:
select t.*
from t
where right(field, 2) = '_1';
You can use like but you need to escape the _:
where field like '%$_1' escape '$'
Or use regular expressions:
where field regexp '_1$'
The underscore character has special significance in a LIKE clause. It acts as a wildcard and represent one single character. So you would have to escape it with a backslash:
LIKE '%\_1'
RIGHT does the job too, but it requires that you provide the proper length for the string being sought and is thus less flexible.
Duh, I found the answer.
Use RIGHT (col_name, 2) = '_1'
Thank you!

Replacing single quote characters in a column in Amazon Redshift

I have some data in a table in the following format in Amazon Redshift:
Column1 Column2
'a' 'b'
I'd like to remove the single quote characters from this table and get the data as follows:
Column1 Column2
a b
I would have thought the Replace function would do the trick, so I wrote the following query:
select replace(column1,''',''),
replace(column2,''','')
from table
But this doesn't work and gives me Amazon](500310) Invalid operation: unterminated quoted string at or near "''',''). I tried to escape the single quote character by \ but even that didn't work.
I also tried using the following query:
select replace(column1,"'",''),
replace(column2,"'",'')
from table
But it gave me the error [Amazon](500310) Invalid operation: column "'" does not exist in <tablename>
So how do I remove these single characters from my data?
Any help would be much appreciated.
TIA.
select replace(column1,chr(39),''),
replace(column2,chr(39),'')
from table
The CHR function returns the character that matches the ASCII code point value specified by of the input parameter. If I’ve made a bad assumption please comment and I’ll refocus my answer.
With MySQL you have two ways of quoting, so you need to switch:
REPLACE(column1, "'", "")
You can't use the same character for both delimiting and content without escaping:
REPLACE(column1, '\'', '')

Replace a part of a file path in a string field with SQL

Hello I have a table Gallery with a field url_immagine and I would like to use a query to replace all values that look like upload/gallery/311/ge_c1966615153f6b2fcf5d84c1e389eea8.jpg in /ge_c1966615153f6b2fcf5d84c1e389eea8.jpg
Unfortunately the a part of the string, the ID (331) is not always the same and therefore can not understand how ...
I tried the regular expression like this:
UPDATE gallery SET url_immagine = replace(url_immagine, 'upload/gallery/.*/', '/')
but it seem not to work.
Combine CONCAT and SUBSTRING_INDEX since you can use last index of "/"
UPDATE gallery
SET url_immagine = (SELECT CONCAT('/',SUBSTRING_INDEX(url_immagine, '/', -1)));
Try that to confirm it's doing what you want :
SELECT CONCAT('/',SUBSTRING_INDEX(url_immagine, '/', -1))
FROM gallery
You can see documentation for the replace function and all other string functions in the mysql manual:
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
It does not mention that replace handles regular expressions, so we an assume it does not, and it is working verbatim and uses the your * to look for the char *.
You see also that there seem not to be a function that does the whole job for you. So you must somehow combine them. The idea of Mateo is probably the right direction.

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, '\'', '&apos;');
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 &apos; in place of '.
This will work:
Replace(my_column,"'",'&apos;')

mysql: replace \ (backslash) in strings

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?