SQL new column with pattern match

╄→гoц情女王★ 提交于 2019-12-25 06:52:04

问题


I have a column with a url sting that looks like this

http://www.somedomain.edu/rootsite1/something/something/ or http://www.somedomain.edu/sites/rootsite2/something/something

Basically I want to ONLY return the string up to root site (in another column).. root site can be anyting (but /sites), but it will either follow /sites/ or .edu/

so the above two strings would return:

http://www.somedomain.edu/rootsite1
http://www.somedomain.edu/sites/rootsite2

I can't compile the view with CLR, so I don't think Regex is an option.

Thanks for any help.


回答1:


I think you'll do better by splitting up the URL on the client side and saving it as two pieces in the table (one containing the "root" site, the other containing the site-specific path), then putting them back together again on the client side after retrieval.

If you choose to store them in the table as you describe above, you can use CHARINDEX to determine where the .edu or /sites/ occurs in the string, then use SUBSTRING to break it up based on that index.

If you really need to do this, here's an example:

declare @sites table (URL varchar(500))

insert into @sites
values
('http://www.somedomain.edu/rootsite1/something/something/'),
('http://www.somedomain.edu/sites/rootsite2/something/something')

select
    URL,
    SUBSTRING(URL, 1, case when charindex('/sites/', URL) > 0 then 
        charindex('/', URL, charindex('/sites/', URL) + 7) else 
        charindex('/', URL, charindex('.edu/', URL) + 5) end - 1)

from @sites



回答2:


you could use CHARINDEX, LEN and SUBSTRING to do this although im not sure sql is the best place to do it

DECLARE @testStr VARCHAR(255)
SET @testStr = 'http://www.somedomain.edu/rootsite1/something/something/'

PRINT SUBSTRING(@testStr, 0, CHARINDEX('.edu', @testStr))

Not a full solution but should give you a start



来源:https://stackoverflow.com/questions/8038954/sql-new-column-with-pattern-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!