Concatenation Help

  • Hi,

    i am struggling with concatenating integer values.

    Declare @id1 int,@id2 int,@id3 int;

    sample outputs:

    set @id1 = 60;

    set @id = 10

    set @id3 = 20;

    output: 60,10,20

    set @id1 = 60;

    set @id3 = 20;

    output: 60,20

    set @id1 = 60;

    set @id2 = 10;

    output: 60,10

    my query:

    select cast(@id1 as varchar(10)) + ',' + cast(@id2 as varchar(10)) + ',' +

    cast(@id3 as varchar(10))

    the concept is i need to send the variable values if it has any value

    any sample query please. i tried to use casting the variable to convert as string and using ',' to make it as comma separated. but some times it gives the value as 60,0,0. i don't want to send the 0

  • this is my next try,

    set @id1 = 60

    select coalesce(cast(@id1 as varchar(10)),'') + ',' + coalesce(cast(@id1 as varchar(10)),'') + ',' +

    coalesce(cast(@id1 as varchar(10)),'')

    it gives the result as 60,,

    I don't want the ,,. because it it has value result with comma. if it doesn't, no need to append comma

    Any help please

  • Some time mind doesn't working to get easy logic

    Here is the way i achieved.

    select

    stuff

    (

    coalesce(', ' + cast(@id1 as varchar(10)), '') +

    coalesce(', ' + cast(@id2 as varchar(10)), '') +

    coalesce(', ' + cast(@id3 as varchar(10)), '') , 1, 2, ''

    ) as result

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply