Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have strings that have really only one rule: There will always be a delimiter of '/' with a frequency of 2.

Examples:

1/2/3

111/222222/3

asdf/dd/eds

How can I do this without string_split() or making a new table to split this string into 3?

I've made several attempts, but there is always something slightly incorrect.

Assume for the example below that 'str' is one of the samples mentioned above.

select
    substring(str,0,charindex('/',str,0)) as first, 
    substring(str,charindex('/',str,0) + 1,charindex('/',strcharindex('/',strcharindex('/',str,0) + 1))) as 2nd,
    --3rd
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

The simplest solution in this case would be to (ab)use parsename - using cross apply and replace:

SELECT PARSENAME(val, 3) As col1,
       PARSENAME(val, 2) As col2,
       PARSENAME(val, 1) As col3
FROM Table
CROSS APPLY
(
    SELECT REPLACE(str, '/', '.') As val
) x

Following Martin Smith's comment - This will only work if the strings between the delimiters are no longer than 128 chars, and if the don't contain dots (though that's probably fixable with more replace statements, but that's going to make the entire thing more cumbersome)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...