Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:08:33+00:00 2026-05-23T18:08:33+00:00

I’ve been trying to write a Table-Valued function that takes value pairs as a

  • 0

I’ve been trying to write a Table-Valued function that takes value pairs as a parameter and return a table with two columns.

Below is the function signature I am trying to do.

FUNCTION [dbo].[ValuePairParser]( @DelimitedValuePairs VARCHAR(MAX),
                                  @Delimiter CHAR(1), 
                                  @ValuePairDelimiter CHAR(1) ) 
  RETURNS @ValuePairTable
  TABLE ( Id INT, Code INT ) 

I want to call the method like below

@ValuePairs VARCHAR(MAX) = '1:1, 1:2, 1:4, 2:3, 1000:230, 130:120,'

ValuePairParser (@ValuePairs, ',', ':')

Can you see any nice way to split above ValuePairs sting and create a table with two columns?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:08:34+00:00Added an answer on May 23, 2026 at 6:08 pm

    Revisiting after a decade there are definitely better ways to do this today.

    SQL Server 2022, Azure SQL Database, Managed Instance

    (example)

    CREATE OR ALTER FUNCTION dbo.SplitWithPairs
    (
        @List           nvarchar(max),
        @MajorDelimiter varchar(3) = ',',
        @MinorDelimiter varchar(3) = ':'
    )
    RETURNS TABLE WITH SCHEMABINDING
    AS
      RETURN 
      (
        SELECT LeftItem = [1], RightItem = [2], Position
        FROM
        (
          SELECT Position = o.ordinal, 
                 value    = TRIM(i.value),
                 i.ordinal
            FROM        STRING_SPLIT(@List,   @MajorDelimiter, 1) AS o
            CROSS APPLY STRING_SPLIT(o.value, @MinorDelimiter, 1) AS i
          WHERE o.value > ''
        ) AS s PIVOT (MAX(value) FOR ordinal IN ([1],[2])) AS p
      );
    

    SQL Server 2016 – 2019

    (example)

    In SQL Server 2016 through 2019, a few changes:

    • STRING_SPLIT doesn’t support ordinal until SQL Server 2022, so we can use OPENJSON instead
    • we need to use LTRIM/RTRIM since TRIM was added in SQL Server 2017
    • key is 0-based so we bump by 1 to get an equivalent 1-based position

    One note about this function is that you can’t use , as the minor delimiter without changing the function, since , is how OPENJSON delimits. There are probably other characters you may want to stay from for both delimiters.

    CREATE OR ALTER FUNCTION dbo.SplitWithPairs
    (
        @List           nvarchar(max),
        @MajorDelimiter varchar(3) = ',',
        @MinorDelimiter varchar(3) = ':' -- can't be ,
    )
    RETURNS TABLE WITH SCHEMABINDING
    AS
      RETURN 
      (
        SELECT LeftItem = [1], RightItem = [2], Position
        FROM
        (  
          SELECT Position = o.[key] + 1, 
                 value    = LTRIM(RTRIM(i.value)),
                 ordinal  = i.[key] + 1
          FROM OPENJSON
          (
            CONCAT('["', REPLACE(STRING_ESCAPE(@List, 'JSON'), 
              @MajorDelimiter, '","'), '"]')
          ) AS o
          CROSS APPLY OPENJSON
          (
            REPLACE(CONCAT('[', QUOTENAME(o.value, '"'), ']'), 
              @MinorDelimiter, '","')
          ) AS i
          WHERE o.value > ''
        ) AS s PIVOT (MAX(value) FOR ordinal IN ([1],[2])) AS p
      );
    

    Versions no longer supported

    And finally, the original answer, which will work on versions older than SQL Server 2016, I’ll leave as it was, but with a disclaimer that it could be improved (no shortage of reading about there here and, generally, multi-statement table-valued functions – particularly with loops – are a big red flag):

    CREATE FUNCTION [dbo].[SplitWithPairs]
    (
        @List NVARCHAR(MAX),
        @MajorDelimiter VARCHAR(3) = ',',
        @MinorDelimiter VARCHAR(3) = ':'
    )
    RETURNS @Items TABLE
    (
        Position  INT IDENTITY(1,1) NOT NULL,
        LeftItem  INT NOT NULL,
        RightItem INT NOT NULL
    )
    AS
    BEGIN
        DECLARE
            @Item      NVARCHAR(MAX),
            @LeftItem  NVARCHAR(MAX),
            @RightItem NVARCHAR(MAX),
            @Pos       INT;
    
        SELECT
            @List = @List + ' ',
            @MajorDelimiter = LTRIM(RTRIM(@MajorDelimiter)),
            @MinorDelimiter = LTRIM(RTRIM(@MinorDelimiter));
    
        WHILE LEN(@List) > 0
        BEGIN
            SET @Pos = CHARINDEX(@MajorDelimiter, @List);
    
            IF @Pos = 0 
                SET @Pos = LEN(@List) + LEN(@MajorDelimiter);
    
            SELECT
                @Item = LTRIM(RTRIM(LEFT(@List, @Pos - 1))),
                @LeftItem = LTRIM(RTRIM(LEFT(@Item,
                CHARINDEX(@MinorDelimiter, @Item) - 1))),
                @RightItem = LTRIM(RTRIM(SUBSTRING(@Item,
                CHARINDEX(@MinorDelimiter, @Item)
                + LEN(@MinorDelimiter), LEN(@Item))));
    
            INSERT @Items(LeftItem, RightItem)
                SELECT @LeftItem, @RightItem;
    
            SET @List = SUBSTRING(@List,
                @Pos + LEN(@MajorDelimiter), DATALENGTH(@List));
        END
        RETURN;
    END
    GO
    
    DECLARE @ValuePairs VARCHAR(MAX) = '1:1, 1:2, 1:4, 2:3,1000:230, 130:120,';
    
    SELECT LeftItem, RightItem
      FROM dbo.SplitWithPairs(@ValuePairs, ',', ':')
      ORDER BY Position;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.