When concatenating using COALESCE, number more than 9 displays as asterisk *









up vote
0
down vote

favorite












I want to concatenate values from multiple rows into one. I am using COALESCE for this purpose. One of the columns I have is an ID column. When concatenating ID column, values up to 9 are displayed correctly but after nine, asterisk is displayed. Anyone knows why this is? See my code below using COALESCE to concatenate all rows in one:



CREATE TABLE #test
(id int, name varchar(50))
insert into #test
values(1, 'ana'),
(2, 'bob'),
(3, 'steph'),
(4, 'bill'),
(5, 'john'),
(6, 'jose'),
(7, 'kerry'),
(8, 'frank'),
(9, 'noah'),
(10, 'melissa')

--SELECT * FROM #test

DECLARE @NameAndID VARCHAR(1000)
SELECT @NameAndID = COALESCE(@NameAndID +'; ', '') + CAST(ID AS VARCHAR(1))+'. ' + name
FROM #test

SELECT @NameAndID









share|improve this question



















  • 5




    Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
    – dnoeth
    Nov 9 at 21:08











  • What is your desired result based on your sample data?
    – Andrew
    Nov 9 at 21:08










  • @dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
    – Stephanie
    Nov 9 at 21:13














up vote
0
down vote

favorite












I want to concatenate values from multiple rows into one. I am using COALESCE for this purpose. One of the columns I have is an ID column. When concatenating ID column, values up to 9 are displayed correctly but after nine, asterisk is displayed. Anyone knows why this is? See my code below using COALESCE to concatenate all rows in one:



CREATE TABLE #test
(id int, name varchar(50))
insert into #test
values(1, 'ana'),
(2, 'bob'),
(3, 'steph'),
(4, 'bill'),
(5, 'john'),
(6, 'jose'),
(7, 'kerry'),
(8, 'frank'),
(9, 'noah'),
(10, 'melissa')

--SELECT * FROM #test

DECLARE @NameAndID VARCHAR(1000)
SELECT @NameAndID = COALESCE(@NameAndID +'; ', '') + CAST(ID AS VARCHAR(1))+'. ' + name
FROM #test

SELECT @NameAndID









share|improve this question



















  • 5




    Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
    – dnoeth
    Nov 9 at 21:08











  • What is your desired result based on your sample data?
    – Andrew
    Nov 9 at 21:08










  • @dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
    – Stephanie
    Nov 9 at 21:13












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to concatenate values from multiple rows into one. I am using COALESCE for this purpose. One of the columns I have is an ID column. When concatenating ID column, values up to 9 are displayed correctly but after nine, asterisk is displayed. Anyone knows why this is? See my code below using COALESCE to concatenate all rows in one:



CREATE TABLE #test
(id int, name varchar(50))
insert into #test
values(1, 'ana'),
(2, 'bob'),
(3, 'steph'),
(4, 'bill'),
(5, 'john'),
(6, 'jose'),
(7, 'kerry'),
(8, 'frank'),
(9, 'noah'),
(10, 'melissa')

--SELECT * FROM #test

DECLARE @NameAndID VARCHAR(1000)
SELECT @NameAndID = COALESCE(@NameAndID +'; ', '') + CAST(ID AS VARCHAR(1))+'. ' + name
FROM #test

SELECT @NameAndID









share|improve this question















I want to concatenate values from multiple rows into one. I am using COALESCE for this purpose. One of the columns I have is an ID column. When concatenating ID column, values up to 9 are displayed correctly but after nine, asterisk is displayed. Anyone knows why this is? See my code below using COALESCE to concatenate all rows in one:



CREATE TABLE #test
(id int, name varchar(50))
insert into #test
values(1, 'ana'),
(2, 'bob'),
(3, 'steph'),
(4, 'bill'),
(5, 'john'),
(6, 'jose'),
(7, 'kerry'),
(8, 'frank'),
(9, 'noah'),
(10, 'melissa')

--SELECT * FROM #test

DECLARE @NameAndID VARCHAR(1000)
SELECT @NameAndID = COALESCE(@NameAndID +'; ', '') + CAST(ID AS VARCHAR(1))+'. ' + name
FROM #test

SELECT @NameAndID






sql sql-server tsql numbers concatenation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:22









Salman A

171k65328414




171k65328414










asked Nov 9 at 21:05









Stephanie

190115




190115







  • 5




    Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
    – dnoeth
    Nov 9 at 21:08











  • What is your desired result based on your sample data?
    – Andrew
    Nov 9 at 21:08










  • @dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
    – Stephanie
    Nov 9 at 21:13












  • 5




    Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
    – dnoeth
    Nov 9 at 21:08











  • What is your desired result based on your sample data?
    – Andrew
    Nov 9 at 21:08










  • @dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
    – Stephanie
    Nov 9 at 21:13







5




5




Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
– dnoeth
Nov 9 at 21:08





Well, a VarChar(1) can only hold a single digit and 10 is clearly two digits. To avoid truncating the result it's returned as *
– dnoeth
Nov 9 at 21:08













What is your desired result based on your sample data?
– Andrew
Nov 9 at 21:08




What is your desired result based on your sample data?
– Andrew
Nov 9 at 21:08












@dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
– Stephanie
Nov 9 at 21:13




@dnoeth This is it!!! Can't believe I spent so much time on this and couldn't see the length of 1..
– Stephanie
Nov 9 at 21:13












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You are casting the number to varchar(1) - and any number that have more than a single digit will overflow the one char and therefor will be turned into an asterisks (*).



When casting ints, I find it best to use varchar(11), since this covers the maximum amount of chars that might be needed to display an int.

The int minimum value is -2,147,483,648 - removing the thousands separators it's 10 digits and a minus sign:



-2147483648
123456789 1 (10 is missing in the chars count to make it more clear)


By the way, there are better ways of doing string aggregation in T-Sql.



For versions prior to 2017, use a combination of stuff and for xml path, like this:



SELECT STUFF(
(
SELECT '; ' + CAST(id as varchar(11)) + '. ' + name
FROM #test
FOR XML PATH('')
),1 ,2, '')


For version 2017 or higher, use the built in string_agg function, like this:



SELECT STRING_AGG(CAST(id as varchar(11)) + '. '+ name, '; ')
FROM #Test


for more information, check out this SO post.






share|improve this answer






















  • Thank you so much, this is it!!!
    – Stephanie
    Nov 9 at 21:12










  • Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
    – Zohar Peled
    Nov 9 at 21:23

















up vote
1
down vote













The * is an indicator that the result length (was) too short to display. In your example you're trying to fit a two digit number into VARCHAR(1). In this particular case the result is the * instead of throwing an error.



The behavior is described in the docs.






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',
    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%2f53233257%2fwhen-concatenating-using-coalesce-number-more-than-9-displays-as-asterisk%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You are casting the number to varchar(1) - and any number that have more than a single digit will overflow the one char and therefor will be turned into an asterisks (*).



    When casting ints, I find it best to use varchar(11), since this covers the maximum amount of chars that might be needed to display an int.

    The int minimum value is -2,147,483,648 - removing the thousands separators it's 10 digits and a minus sign:



    -2147483648
    123456789 1 (10 is missing in the chars count to make it more clear)


    By the way, there are better ways of doing string aggregation in T-Sql.



    For versions prior to 2017, use a combination of stuff and for xml path, like this:



    SELECT STUFF(
    (
    SELECT '; ' + CAST(id as varchar(11)) + '. ' + name
    FROM #test
    FOR XML PATH('')
    ),1 ,2, '')


    For version 2017 or higher, use the built in string_agg function, like this:



    SELECT STRING_AGG(CAST(id as varchar(11)) + '. '+ name, '; ')
    FROM #Test


    for more information, check out this SO post.






    share|improve this answer






















    • Thank you so much, this is it!!!
      – Stephanie
      Nov 9 at 21:12










    • Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
      – Zohar Peled
      Nov 9 at 21:23














    up vote
    1
    down vote



    accepted










    You are casting the number to varchar(1) - and any number that have more than a single digit will overflow the one char and therefor will be turned into an asterisks (*).



    When casting ints, I find it best to use varchar(11), since this covers the maximum amount of chars that might be needed to display an int.

    The int minimum value is -2,147,483,648 - removing the thousands separators it's 10 digits and a minus sign:



    -2147483648
    123456789 1 (10 is missing in the chars count to make it more clear)


    By the way, there are better ways of doing string aggregation in T-Sql.



    For versions prior to 2017, use a combination of stuff and for xml path, like this:



    SELECT STUFF(
    (
    SELECT '; ' + CAST(id as varchar(11)) + '. ' + name
    FROM #test
    FOR XML PATH('')
    ),1 ,2, '')


    For version 2017 or higher, use the built in string_agg function, like this:



    SELECT STRING_AGG(CAST(id as varchar(11)) + '. '+ name, '; ')
    FROM #Test


    for more information, check out this SO post.






    share|improve this answer






















    • Thank you so much, this is it!!!
      – Stephanie
      Nov 9 at 21:12










    • Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
      – Zohar Peled
      Nov 9 at 21:23












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You are casting the number to varchar(1) - and any number that have more than a single digit will overflow the one char and therefor will be turned into an asterisks (*).



    When casting ints, I find it best to use varchar(11), since this covers the maximum amount of chars that might be needed to display an int.

    The int minimum value is -2,147,483,648 - removing the thousands separators it's 10 digits and a minus sign:



    -2147483648
    123456789 1 (10 is missing in the chars count to make it more clear)


    By the way, there are better ways of doing string aggregation in T-Sql.



    For versions prior to 2017, use a combination of stuff and for xml path, like this:



    SELECT STUFF(
    (
    SELECT '; ' + CAST(id as varchar(11)) + '. ' + name
    FROM #test
    FOR XML PATH('')
    ),1 ,2, '')


    For version 2017 or higher, use the built in string_agg function, like this:



    SELECT STRING_AGG(CAST(id as varchar(11)) + '. '+ name, '; ')
    FROM #Test


    for more information, check out this SO post.






    share|improve this answer














    You are casting the number to varchar(1) - and any number that have more than a single digit will overflow the one char and therefor will be turned into an asterisks (*).



    When casting ints, I find it best to use varchar(11), since this covers the maximum amount of chars that might be needed to display an int.

    The int minimum value is -2,147,483,648 - removing the thousands separators it's 10 digits and a minus sign:



    -2147483648
    123456789 1 (10 is missing in the chars count to make it more clear)


    By the way, there are better ways of doing string aggregation in T-Sql.



    For versions prior to 2017, use a combination of stuff and for xml path, like this:



    SELECT STUFF(
    (
    SELECT '; ' + CAST(id as varchar(11)) + '. ' + name
    FROM #test
    FOR XML PATH('')
    ),1 ,2, '')


    For version 2017 or higher, use the built in string_agg function, like this:



    SELECT STRING_AGG(CAST(id as varchar(11)) + '. '+ name, '; ')
    FROM #Test


    for more information, check out this SO post.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 21:21

























    answered Nov 9 at 21:10









    Zohar Peled

    51.2k73171




    51.2k73171











    • Thank you so much, this is it!!!
      – Stephanie
      Nov 9 at 21:12










    • Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
      – Zohar Peled
      Nov 9 at 21:23
















    • Thank you so much, this is it!!!
      – Stephanie
      Nov 9 at 21:12










    • Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
      – Zohar Peled
      Nov 9 at 21:23















    Thank you so much, this is it!!!
    – Stephanie
    Nov 9 at 21:12




    Thank you so much, this is it!!!
    – Stephanie
    Nov 9 at 21:12












    Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
    – Zohar Peled
    Nov 9 at 21:23




    Glad to help :-) Be sure to read the link I've edited in and also the link to the relevant documentation in Salman's answer.
    – Zohar Peled
    Nov 9 at 21:23












    up vote
    1
    down vote













    The * is an indicator that the result length (was) too short to display. In your example you're trying to fit a two digit number into VARCHAR(1). In this particular case the result is the * instead of throwing an error.



    The behavior is described in the docs.






    share|improve this answer


























      up vote
      1
      down vote













      The * is an indicator that the result length (was) too short to display. In your example you're trying to fit a two digit number into VARCHAR(1). In this particular case the result is the * instead of throwing an error.



      The behavior is described in the docs.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        The * is an indicator that the result length (was) too short to display. In your example you're trying to fit a two digit number into VARCHAR(1). In this particular case the result is the * instead of throwing an error.



        The behavior is described in the docs.






        share|improve this answer














        The * is an indicator that the result length (was) too short to display. In your example you're trying to fit a two digit number into VARCHAR(1). In this particular case the result is the * instead of throwing an error.



        The behavior is described in the docs.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 15:26

























        answered Nov 9 at 21:13









        Salman A

        171k65328414




        171k65328414



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233257%2fwhen-concatenating-using-coalesce-number-more-than-9-displays-as-asterisk%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo