Split string and replace
I have a varchar
column with Url's with data tha开发者_如何学运维t looks like this:
http://google.mews.......http://www.somesite.com
I want to get rid of the first http and keep the second one so the row above would result in:
http://www.somesite.com
I've tried using split()
but can't get it to work
Thanks
If you are trying to do this using T-SQL, you can try something in the lines of:
-- assume @v is the variable holding the URL
SELECT SUBSTRING(@v, PATINDEX('%_http://%', @v) + 1, LEN(@v))
This will return the start position of the first http:// that has before it at least one character (hence the '%_' before it and the + 1 offset).
If the first URL always starts right from the beginning of the string, you can use SUBSTRING()
& CHARINDEX()
:
SELECT SUBSTRING(column, CHARINDEX('http://', column, 2), LEN(column))
FROM table
CHARINDEX
simply searches a string for a substring and returns the substring's starting position within the string. Its third argument is optional and, if set, specifies the search starting position, in this case it's 2
so it didn't hit the first http://
.
精彩评论