Function which returns a TABLE after splitting a string

Forey

I want to create function which returns a table with two columns extracted from an input string.

As an input, the function gets a string like:

'@Name=John;@Secondname=Kowalsky;@[email protected];'

The function is supposed to create from this string two columns - x, y.

X for @value and Y for value after =.

I created this:

Create FUNCTION TwoColumnsFromString (
    @ReplaceString varchar(max)
)
Returns @temptable TABLE (x varchar(max), y varchar(max))
AS 
BEGIN
    DECLARE @value1 varchar(max) 
    WHILE (LEN(@value1)) > 0
    BEGIN    
        SELECT  @value1 = (Select @ReplaceString)
        INSERT INTO @temptable (x,y)
        VALUES ((SUBSTRING(@ReplaceString, CHARINDEX('@', @ReplaceString), CHARINDEX('=',@ReplaceString) -CHARINDEX('@',@ReplaceString))),
            (SUBSTRING(@ReplaceString, CHARINDEX('=', @ReplaceString)+1, CHARINDEX(';',@ReplaceString) -CHARINDEX('=',@ReplaceString)-1)))

        SET @value1 = REPLACE(@value1, SUBSTRING(@value1, 1, CHARINDEX(';', @value1)), '')
    END
    RETURN
END
GO

SELECT *
FROM TwoColumnsFromString('@Name=John;@Secondname=Kowalsky;@[email protected];')

But that returns an empty table. What am I doing wrong here?

Dale K

You lost all your good work from your previous question :)

  1. You should assign @value1 its starting value outside your loop
  2. Your insert computations should now reference @value1

Your function body should be

    BEGIN
      -- Set starting value outside the loop
      DECLARE @value1 VARCHAR(max) = @ReplaceString;

      WHILE (LEN(@value1)) > 0
      BEGIN
          -- We are now using @value1 not @ReplaceString
          INSERT INTO @temptable (x,y)
          VALUES ((SUBSTRING(@value1, CHARINDEX('@', @value1), CHARINDEX('=',@value1) -CHARINDEX('@',@value1))),
                  (SUBSTRING(@value1, CHARINDEX('=', @value1)+1, CHARINDEX(';',@value1) -CHARINDEX('=',@value1)-1)));

          SET @value1 = REPLACE(@value1, SUBSTRING(@value1, 1, CHARINDEX(';', @value1)), '');
      END
      RETURN;
    END

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Splitting multiline string into arrays in a function

Junit test function which returns a string

Split method splitting by a string which includes spaces

Splitting a string after certain characters?

write a function which accepts a string and returns a new string with only the capital letters passed to the string

splitting a string misses the word which is used to split it

User defined function returns value error which should be string

Problem with splitting string into tokens with strtok - Char arrays are empty after splitting

Implementing a function which returns function definition as string

Splitting strings after scraping table from data

pinvoke to function which returns string array

Function which returns function scheme

Returning array in Javascript function after splitting string

Unable to compile function which returns parts of a string

Running a string splitting function in a loop

How to use 'string to table splitting' using a function in a query?

Splitting string by space not returns all of the elements

Splitting String into an Array from Function

Jasmine, AngularJS: Unit test a function which returns a value after timeout

function which returns true is the string is in upper-case using XSLT

Generalizing the conditions after splitting of a string

Create a function which takes in a float as input and returns a string containing a number

Splitting string in column in table which has multiple values into separate rows

TypeScript: function that returns a generic Type which extends Record<string, string>

ArrayIndexOutOfBoundsException after splitting a string

Backfill values after splitting one column in table

Splitting a string which contains mathematical operators in python

Splitting String which can contains delimeter

A function that returns a string which resemples a playing board in python

TOP Ranking

HotTag

Archive