You tagged your question with SQL so my answer shows you the tools to use to get what you want using Microsoft Transact SQL.
Use the Microsoft Transact SQL String functions: CHARINDEX and SUBSTRING.
* CHARINDEX to locate the "/" characters.
* SUBSTRING to extract the directory name using the index values returned by CHARINDEX.
This example contains no error checking. When used in an application, there should be error checking.
declare @URL varchar(255);
declare @idx1 int;
declare @str1 varchar(255);
set @URL='http://www.codeproject.com/Questions/ask.aspx';
set @idx1=CHARINDEX('//',@URL);
set @str1=SUBSTRING(@URL,@IDX1+2,255);
set @idx1=CHARINDEX('/',@str1);
set @str1=SUBSTRING(@str1,@IDX1+1,255);
set @idx1=CHARINDEX('/',@str1);
set @str1=SUBSTRING(@str1,0,@idx1);
select @str1;
The above code could be used in a scalar-valued function that could be called from a
SELECT
statement. The following example creates a scalar-valued function. Note: In an application, there should be error checking code.
use MyDatabaseName
go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION URLTopLevelFolder(@URL varchar(255))
RETURNS varchar(255)
AS
BEGIN
declare @idx1 int;
declare @str1 varchar(255);
set @idx1=CHARINDEX('//',@URL);
set @str1=SUBSTRING(@URL,@IDX1+2,255);
set @idx1=CHARINDEX('/',@str1);
set @str1=SUBSTRING(@str1,@IDX1+1,255);
set @idx1=CHARINDEX('/',@str1);
set @str1=SUBSTRING(@str1,0,@idx1);
RETURN @str1
END
GO
Example of use of the scalar-valued function
select dbo.URLTopLevelFolder('http://www.codeproject.com/Questions/ask.aspx')
It might provide better performance to do this type of character string handling/extraction in the application programming language rather than in the database server.