简体   繁体   中英

How to select string between characters?

I made this table:

Table (Websites)

WebsiteID | WebsiteName
2324442     'http://www.samsung.com/us/'
2342343     'https://www.microsoft.com/en-au/windows/'
3242343     'http://www.apple.com/au/iphone/'

And I want to be able to SELECT the domain names from this table.

Something like this:

WebsiteName
'www.samsung.com'
'www.microsoft.com'
'www.apple.com'

Is there a string method I can use for this? Like splitting the string between // and / .

You can use SUBSTRING_INDEX() :

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(websiteName, '//', -1),
                   '/', 1)
FROM table

You can even use the following:

SELECT WebsiteID , WebsiteName
(CHARINDEX ( '//', WebsiteName, 1 ) + 1), -- Position of the double slashes
CHARINDEX( '/', REVERSE (WebsiteName ), 1), -- Position of the last single slash
SUBSTRING(WebsiteName, (CHARINDEX ( '//' ,  WebsiteName, 1 ) + 2), CHARINDEX( '/', REVERSE (WebsiteName ), 1) ) -- Final string
FROM Table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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