Link to home
Start Free TrialLog in
Avatar of AbeSpain
AbeSpainFlag for United Kingdom of Great Britain and Northern Ireland

asked on

String concatenation inside a loop is not returning what I expect

I have the following function that simply returns a number of hyphens equal to the argument length passed in to the function. However, it only returns one hyphen.
Alter Function CreateWhiteSpace (@WhiteSpaceCount Int)
Returns char(32)
As
Begin
Declare @I Int
Declare @WhiteSpace Char(32)
Set @I = 0 
 
	While @I <= @WhiteSpaceCount
		Begin
			Set @WhiteSpace = COALESCE(@WhiteSpace,'-') + '-'
			Set @I = @I + 1
		End
 
return @WhiteSpace
End

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AbeSpain

ASKER

hah, my bad! As simple as that, gee. I wouldn't mind I only ever really use varchar normally. I think I copied an old function and left char in!
No problem.  I often reuse code and so I know all about the woes of copy and paste.  :) Glad that was it.