简体   繁体   English

SQL查询查找和修剪

[英]SQL Query Find & Trim

I have a URL (example: https://mycompoany.com/data/test ) and want to remove everything left of test. 我有一个URL(例如: https : //mycompoany.com/data/test ),并且想要删除测试的所有内容。 What is the best way to do this? 做这个的最好方式是什么?

I was going to use like the following. 我打算像下面这样使用。 However, I'm not sure how to return the length of the last /. 但是,我不确定如何返回最后一个/的长度。

LEN(LTRIM(THE_TEXT))

You can use SUBSTRING_INDEX() with -1 for the counter argument like this: 您可以将SUBSTRING_INDEX()与-1一起用作计数器参数,如下所示:

SUBSTRING_INDEX(THE_TEXT, '/', -1)

For example, this line: 例如,这一行:

SELECT SUBSTRING_INDEX('https://mycompoany.com/data/test', '/', -1)

returns test . 返回test

Thank you everyone for the assistance. 谢谢大家的帮助。 It is greatly appreciated. 非常感谢。 Here is what I ended up doing. 这就是我最终要做的。

SELECT top 10 profile_url,
    CASE
        WHEN CHARINDEX('/', profile_url) > 0 THEN
            RIGHT(profile_url, CHARINDEX('/', REVERSE(profile_url))-1)
        ELSE profile_url
    END
FROM connections

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

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