How to get the element from a delimited string by its index?

s.dragos

I have this function (credit: searchsqlserver):

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000)             -- List of delimited items
    , @sDelimiter VARCHAR(8000) = ','     -- delimiter that separates items
 ) RETURNS @List TABLE (item VARCHAR(8000))
     BEGIN
     DECLARE @sItem VARCHAR(8000)
     WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
         BEGIN
         SELECT
         @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,
            @sInputList,0)-1))),
         @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,
            @sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
         IF LEN(@sItem) > 0
            INSERT INTO @List SELECT @sItem
         END

     IF LEN(@sInputList)> 0
     INSERT INTO @List SELECT @sInputList -- Put the last item in
     RETURN
     END
 GO

It takes as parameters a string and a delimiter and returns the delimited elements one by one.

select * from fnSplit('1,22,333', ',')    --   returns 1 22 333

I'll confess that I`m new to SQL and I simply can't follow the whole logic behind this function. What I'm trying to achieve is a function that has a third parameter(an index) and returns the element on the position mention by the index. For example:

select * from fnSplit('1 22 333 444 5555 666', ' ' , 2 ) --  333

select * from fnSplit('1 22 333 444 5555 666', ' ' , 0 ) --  1
Lukasz Szozda

Using multistatment table-valued UDF with loop to parse string is very inefficient. Better approaches: Split strings the right way – or the next best way


Anyway if you want to adapt your function you could set IDENTITY column for table variable and then filter based on third parameter:

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000)             -- List of delimited items
    , @sDelimiter VARCHAR(8000) = ','     -- delimiter that separates items
    ,@num INT
 ) RETURNS @List TABLE ( item VARCHAR(8000))
     BEGIN
     DECLARE @ListHelper AS TABLE(id INT IDENTITY(1,1), item VARCHAR(8000));
     DECLARE @sItem VARCHAR(8000)
     WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
         BEGIN
         SELECT
         @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,
            @sInputList,0)-1))),
         @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,
            @sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
         IF LEN(@sItem) > 0
            INSERT INTO @ListHelper SELECT @sItem
         END

     IF LEN(@sInputList)> 0
     INSERT INTO @ListHelper SELECT @sInputList -- Put the last item in
     INSERT INTO @List
     SELECT item
     FROM @ListHelper
     WHERE id = @num
     RETURN
     END
 GO

select * from fnSplit('1 22 333 444 5555 666', ' ' , 3 );
--333

LiveDemo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can print big string from a string array or get its index

Get an element with its index counting from the end in Kotlin

How to get element of array with string index path

How get jQuery UI Tabs DOM element by its index?

How to get input element value by its index number

How to get type from its string representation

How to get the index of element from body tag?

How to get array index from html element?

How to check if an item exists in a string List and if it exists, get its index

How to get value of a variable in a long delimited string

How to get the index of Differences in String from index - to index?

How to get frame from video by its index via OpenCV and Python?

Java - How to get item index from an array with its value?

How to strip a value from a delimited string

How to get the last index from a String in a for loop

How to get char from string by index?

How to get an Int from a string.index?

How to get a CSS property of an HTML element from its class?

How to get an element from li value and manipulate its value?

how get element from List based on its name?

How to get a HTML element from a string with jQuery

How to get raw xml as string from Element

How to get an element from a text string in R

how to get element from string that was split

Efficient way to extract just one element from a delimited string

Extracting the first element from an String ArrayList delimited with commas

Remove an element from the arrayknowing its index

How do I get quoted fields from a delimited string as a list of unquoted values using LINQ?

How to get element from an index from an array of object student

TOP Ranking

HotTag

Archive