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
sql sql-server tsql numbers concatenation
add a comment |
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
sql sql-server tsql numbers concatenation
5
Well, aVarChar(1)
can only hold a single digit and10
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
add a comment |
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
sql sql-server tsql numbers concatenation
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
sql sql-server tsql numbers concatenation
edited Nov 10 at 15:22
Salman A
171k65328414
171k65328414
asked Nov 9 at 21:05
Stephanie
190115
190115
5
Well, aVarChar(1)
can only hold a single digit and10
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
add a comment |
5
Well, aVarChar(1)
can only hold a single digit and10
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
add a comment |
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.
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
add a comment |
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.
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 15:26
answered Nov 9 at 21:13
Salman A
171k65328414
171k65328414
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
5
Well, a
VarChar(1)
can only hold a single digit and10
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