简体   繁体   中英

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.

eg

SELECT FILE_NAME, REPLACE (FILE_NAME,'\\nt4\\applications\\prod\\hde\\atn\\','') as newfilename from atn_documents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM