简体   繁体   English

替换MySQL Select查询中一部分列值

[英]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. 我已经阅读了论坛,以找到针对我的问题的解决方案,但是使用查询时,我陷入了MySQL错误。 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: 我想提取字段的一部分,在FILE_NAME列中\\\\nt4\\applications\\prod\\hde\\atn\\之后的所有内容,这是查询:

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 : 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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