SELECT REPLACE([strUrl], '/myoldurl', '/mynewurl') FROM [UrlRewrite]

如果 strUrl/myoldurl/myoldurl_something_else ,则返回 /mynewurl/mynewurl_something_else 。我希望它是 /mynewurl/myoldurl_something_else

如何在 SQL 中从字符串的开头替换?

最佳答案

SELECT REPLACE([strUrl], '/myoldurl/', '/mynewurl/') FROM [UrlRewrite]

解决马丁的观点:
SELECT CASE WHEN [strUrl] = '/myoldurl' THEN '/mynewurl' ELSE REPLACE([strUrl], '/myoldurl/', '/mynewurl/') END FROM [UrlRewrite]

或者
SELECT
    CASE
    WHEN LEFT([strUrl], LEN('/myoldurl')) = '/myoldurl'
        THEN '/mynewurl' + SUBSTRING([strUrl], LEN('/myoldurl')+1, LEN([strUrl]))
    ELSE [strUrl]
    END
FROM [UrlRewrite]

关于sql - 替换 SQL 中字符串的开头,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11339801/

10-09 21:00