Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 string adding problem

Author  Topic 

bill_
Starting Member

38 Posts

Posted - 2013-09-18 : 10:21:38
This prints the 10 lines a_,...,a_
but want it to print the 10 lines a_1, a_2, a_3, ..., a_10
What is the error?

declare @ex varchar(4)
set @ex='a_'
while @ind<11 --starts at 1
begin
set @ex=@ex+str(@ind)
print @ex
@ind=@ind+1
end

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-09-18 : 10:28:28
[code]declare @ex varchar(4)
set @ex='a_'
while @ind<11 --starts at 1
begin
set @ex=@ex+convert(varchar(2), @ind)
print @ex
select @ind=@ind+1
end[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-18 : 10:39:47
STR functions defaults at one character.

+STR(@ind)

should be

+STR(@ind, 2) -- or more characters



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

bill_
Starting Member

38 Posts

Posted - 2013-09-25 : 15:32:44
Thank you. Convert() helped.
Go to Top of Page
   

- Advertisement -