如何将SQL查询返回的STRUCTURE字段统一替换为正斜杠格式?
Alright, let's solve this path formatting problem. You need to turn a Windows-style backslash-separated path into a forward-slash one, and the solution is straightforward using SQL's built-in string replacement functions. Here's how to implement it across common database systems:
MySQL/MariaDB
In MySQL, backslashes are escape characters, so you need to double them in the REPLACE function to target a single backslash:
SELECT REPLACE(PATH, '\\', '/') AS STRUCTURE FROM OBJ_RESOURCE WHERE PATH LIKE '%Small Business%';
This query will swap every backslash (\) in the PATH value with a forward slash (/), turning your original string exactly into the desired format: /Small Business/Organisation / IT / Logistik/Organisation / Governance/Person/Prozess-User.
SQL Server
SQL Server handles backslashes a bit differently — you can use a single backslash in the REPLACE function without escaping it (since it's not an escape character in regular string literals here):
SELECT REPLACE(PATH, '\', '/') AS STRUCTURE FROM OBJ_RESOURCE WHERE PATH LIKE '%Small Business%';
This will produce the same forward-slash path you need, converting all backslashes including the leading one.
Oracle
Oracle doesn't treat backslashes as escape characters in string literals, so the REPLACE function works with a single backslash directly:
SELECT REPLACE(PATH, '\', '/') AS STRUCTURE FROM OBJ_RESOURCE WHERE PATH LIKE '%Small Business%';
Just like the other examples, this will fully convert the path to use forward slashes exclusively.
Key Note
All these solutions work because the REPLACE function operates on every occurrence of the target character in the string. That means the leading backslash gets converted to a forward slash, and every subsequent backslash in the path is swapped out too — exactly matching your required output.
内容的提问来源于stack exchange,提问作者anna

