May 26, 2016 at 1:45 pm
I am trying to extract the folder name from the path which usually is after the 2nd delimiter
example
/dev/Mywork/deailsbyTeam
I need to get "Mywork"
please advice
May 26, 2016 at 2:03 pm
Use DelimitedSplit8K and grab the 3rd item?
May 26, 2016 at 2:04 pm
sharonsql2013 (5/26/2016)
I am trying to extract the folder name from the path which usually is after the 2nd delimiterexample
/dev/Mywork/deailsbyTeam
I need to get "Mywork"
please advice
The problem I see is your use of "usually" above. Could you provide more clarity as to what you are looking for here?
May 26, 2016 at 2:16 pm
When you say folder name, is it the root folder or the parent folder?
Can you give several examples? Mainly to get exceptions to the rule. Don't forget to include expected results.
May 26, 2016 at 2:34 pm
As mentioned
It could be /dev/Mywork/deailsbyWork
or /dev/MyTeam/deailsbyTeam
or /dev/MySubgroup/deailsbySubgroup
Outcome
Mywork
MyTeam
MySubgroup
May 26, 2016 at 2:37 pm
sharonsql2013 (5/26/2016)
As mentionedIt could be /dev/Mywork/deailsbyWork
or /dev/MyTeam/deailsbyTeam
or /dev/MySubgroup/deailsbySubgroup
Outcome
Mywork
MyTeam
MySubgroup
Based on the above, usually then mean always, correct?
May 26, 2016 at 2:54 pm
Perhaps this?
with testdata as (
select
Directory
from
(values
('/dev/Mywork/deailsbyWork'),
('/dev/MyTeam/deailsbyTeam'),
('/dev/MySubgroup/deailsbySubgroup'))dt(Directory)
)
select
Directory,
substring(Directory,charindex('/',Directory,2) + 1,(charindex('/',Directory,charindex('/',Directory,2) + 1) - charindex('/',Directory,2)) - 1)
from testdata;
May 26, 2016 at 3:04 pm
Thank you Lynn
May 26, 2016 at 4:53 pm
Alternative method using Lynn's fine test set (with the addition of a Beginninator 😀 )
😎
;WITH testdata AS (
SELECT
Directory
FROM
(VALUES
('/dev/Mywork/deailsbyWork'),
('/dev/MyTeam/deailsbyTeam'),
('/dev/MySubgroup/deailsbySubgroup'))dt(Directory)
)
,FIRST_CHOP AS
(
SELECT
TD.Directory
,SUBSTRING(TD.Directory,CHARINDEX(CHAR(47),TD.Directory,2) + 1,LEN(TD.Directory)) AS PART
FROM testdata TD
)
SELECT
FC.Directory
,SUBSTRING(FC.PART,1,CHARINDEX(CHAR(47),FC.PART,2) - 1) AS DIR_NAME
FROM FIRST_CHOP FC;
Output
Directory DIR_NAME
-------------------------------- ------------
/dev/Mywork/deailsbyWork Mywork
/dev/MyTeam/deailsbyTeam MyTeam
/dev/MySubgroup/deailsbySubgroup MySubgroup
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply