String Split Ignore Last delimiter if no data












0














I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.



Example



Userid |   Value
1 | A,B,C,D,
2 | F,H


Code



select value
from string_split('A,B,C,D,',',')


Current Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D
1 |


Is there any way to make the string split function ignore the final comma if no data follows it?



Desired Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D


Using MSSQL










share|improve this question




















  • 9




    Just add WHERE value <> ''?
    – MatBailie
    Nov 22 at 22:55






  • 2




    That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
    – Squirrel
    Nov 23 at 0:07








  • 1




    So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
    – ZLK
    Nov 23 at 0:47






  • 1




    Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
    – Jack
    Nov 23 at 1:38






  • 1




    You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
    – Damien_The_Unbeliever
    Nov 23 at 10:39
















0














I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.



Example



Userid |   Value
1 | A,B,C,D,
2 | F,H


Code



select value
from string_split('A,B,C,D,',',')


Current Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D
1 |


Is there any way to make the string split function ignore the final comma if no data follows it?



Desired Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D


Using MSSQL










share|improve this question




















  • 9




    Just add WHERE value <> ''?
    – MatBailie
    Nov 22 at 22:55






  • 2




    That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
    – Squirrel
    Nov 23 at 0:07








  • 1




    So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
    – ZLK
    Nov 23 at 0:47






  • 1




    Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
    – Jack
    Nov 23 at 1:38






  • 1




    You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
    – Damien_The_Unbeliever
    Nov 23 at 10:39














0












0








0







I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.



Example



Userid |   Value
1 | A,B,C,D,
2 | F,H


Code



select value
from string_split('A,B,C,D,',',')


Current Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D
1 |


Is there any way to make the string split function ignore the final comma if no data follows it?



Desired Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D


Using MSSQL










share|improve this question















I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.



Example



Userid |   Value
1 | A,B,C,D,
2 | F,H


Code



select value
from string_split('A,B,C,D,',',')


Current Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D
1 |


Is there any way to make the string split function ignore the final comma if no data follows it?



Desired Output



UserId |   Value
1 | A
1 | B
1 | C
1 | D


Using MSSQL







sql sql-server split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 23:05

























asked Nov 22 at 22:51









Vine

506




506








  • 9




    Just add WHERE value <> ''?
    – MatBailie
    Nov 22 at 22:55






  • 2




    That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
    – Squirrel
    Nov 23 at 0:07








  • 1




    So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
    – ZLK
    Nov 23 at 0:47






  • 1




    Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
    – Jack
    Nov 23 at 1:38






  • 1




    You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
    – Damien_The_Unbeliever
    Nov 23 at 10:39














  • 9




    Just add WHERE value <> ''?
    – MatBailie
    Nov 22 at 22:55






  • 2




    That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
    – Squirrel
    Nov 23 at 0:07








  • 1




    So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
    – ZLK
    Nov 23 at 0:47






  • 1




    Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
    – Jack
    Nov 23 at 1:38






  • 1




    You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
    – Damien_The_Unbeliever
    Nov 23 at 10:39








9




9




Just add WHERE value <> ''?
– MatBailie
Nov 22 at 22:55




Just add WHERE value <> ''?
– MatBailie
Nov 22 at 22:55




2




2




That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
– Squirrel
Nov 23 at 0:07






That also removes NULL values. What do you mean by that ? string_split value column will not have null value. And If you pass a NULL value to string_split, it will not return any rows.
– Squirrel
Nov 23 at 0:07






1




1




So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
– ZLK
Nov 23 at 0:47




So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether RIGHT(Value, 1) = ',' and use a substring of the value, otherwise use the full string.
– ZLK
Nov 23 at 0:47




1




1




Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38




Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38




1




1




You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39




You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39












1 Answer
1






active

oldest

votes


















0














STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function



CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)

)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value

INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO


Sample data table



DECLARE @Data AS TABLE(Userid INT ,  Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'


Sql script to get the expected result



SELECT  d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO


Result



Userid  Value
------------
1 A
1 B
1 C
1 D





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438812%2fstring-split-ignore-last-delimiter-if-no-data%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
    Created User defined Function



    CREATE FUNCTION [dbo].[Udf_StringSplit]
    (
    @Userid INT,
    @Value VARCHAR(1000)

    )
    RETURNS @Result TABLE(
    Userid INT,
    Value VARCHAR(10)
    )
    AS BEGIN
    DECLARE @Data AS TABLE
    (
    Userid INT,
    Value VARCHAR(100)
    )
    INSERT INTO @Data(Userid,Value)
    SELECT @Userid, @Value

    INSERT INTO @Result(Userid,Value)
    SELECT Userid,
    Split.a.value('.','nvarchar(1000)') AS Value
    FROM
    (
    SELECT Userid,
    CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
    FROM @Data
    ) AS A
    CROSS APPLY Value.nodes('S') AS Split(a)
    WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
    RETURN
    END
    GO


    Sample data table



    DECLARE @Data AS TABLE(Userid INT ,  Value VARCHAR(100))
    INSERT INTO @Data
    SELECT 1,'A,B,C,D,' UNION ALL
    SELECT 2,'F,H'


    Sql script to get the expected result



    SELECT  d.Userid,
    f.Value
    FROM @Data d
    CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
    WHERE d.Userid=1
    GO


    Result



    Userid  Value
    ------------
    1 A
    1 B
    1 C
    1 D





    share|improve this answer




























      0














      STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
      Created User defined Function



      CREATE FUNCTION [dbo].[Udf_StringSplit]
      (
      @Userid INT,
      @Value VARCHAR(1000)

      )
      RETURNS @Result TABLE(
      Userid INT,
      Value VARCHAR(10)
      )
      AS BEGIN
      DECLARE @Data AS TABLE
      (
      Userid INT,
      Value VARCHAR(100)
      )
      INSERT INTO @Data(Userid,Value)
      SELECT @Userid, @Value

      INSERT INTO @Result(Userid,Value)
      SELECT Userid,
      Split.a.value('.','nvarchar(1000)') AS Value
      FROM
      (
      SELECT Userid,
      CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
      FROM @Data
      ) AS A
      CROSS APPLY Value.nodes('S') AS Split(a)
      WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
      RETURN
      END
      GO


      Sample data table



      DECLARE @Data AS TABLE(Userid INT ,  Value VARCHAR(100))
      INSERT INTO @Data
      SELECT 1,'A,B,C,D,' UNION ALL
      SELECT 2,'F,H'


      Sql script to get the expected result



      SELECT  d.Userid,
      f.Value
      FROM @Data d
      CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
      WHERE d.Userid=1
      GO


      Result



      Userid  Value
      ------------
      1 A
      1 B
      1 C
      1 D





      share|improve this answer


























        0












        0








        0






        STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
        Created User defined Function



        CREATE FUNCTION [dbo].[Udf_StringSplit]
        (
        @Userid INT,
        @Value VARCHAR(1000)

        )
        RETURNS @Result TABLE(
        Userid INT,
        Value VARCHAR(10)
        )
        AS BEGIN
        DECLARE @Data AS TABLE
        (
        Userid INT,
        Value VARCHAR(100)
        )
        INSERT INTO @Data(Userid,Value)
        SELECT @Userid, @Value

        INSERT INTO @Result(Userid,Value)
        SELECT Userid,
        Split.a.value('.','nvarchar(1000)') AS Value
        FROM
        (
        SELECT Userid,
        CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
        FROM @Data
        ) AS A
        CROSS APPLY Value.nodes('S') AS Split(a)
        WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
        RETURN
        END
        GO


        Sample data table



        DECLARE @Data AS TABLE(Userid INT ,  Value VARCHAR(100))
        INSERT INTO @Data
        SELECT 1,'A,B,C,D,' UNION ALL
        SELECT 2,'F,H'


        Sql script to get the expected result



        SELECT  d.Userid,
        f.Value
        FROM @Data d
        CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
        WHERE d.Userid=1
        GO


        Result



        Userid  Value
        ------------
        1 A
        1 B
        1 C
        1 D





        share|improve this answer














        STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
        Created User defined Function



        CREATE FUNCTION [dbo].[Udf_StringSplit]
        (
        @Userid INT,
        @Value VARCHAR(1000)

        )
        RETURNS @Result TABLE(
        Userid INT,
        Value VARCHAR(10)
        )
        AS BEGIN
        DECLARE @Data AS TABLE
        (
        Userid INT,
        Value VARCHAR(100)
        )
        INSERT INTO @Data(Userid,Value)
        SELECT @Userid, @Value

        INSERT INTO @Result(Userid,Value)
        SELECT Userid,
        Split.a.value('.','nvarchar(1000)') AS Value
        FROM
        (
        SELECT Userid,
        CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
        FROM @Data
        ) AS A
        CROSS APPLY Value.nodes('S') AS Split(a)
        WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
        RETURN
        END
        GO


        Sample data table



        DECLARE @Data AS TABLE(Userid INT ,  Value VARCHAR(100))
        INSERT INTO @Data
        SELECT 1,'A,B,C,D,' UNION ALL
        SELECT 2,'F,H'


        Sql script to get the expected result



        SELECT  d.Userid,
        f.Value
        FROM @Data d
        CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
        WHERE d.Userid=1
        GO


        Result



        Userid  Value
        ------------
        1 A
        1 B
        1 C
        1 D






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 at 10:57

























        answered Nov 23 at 10:34









        Sreenu131

        1,300138




        1,300138






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438812%2fstring-split-ignore-last-delimiter-if-no-data%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo