Link to home
Create AccountLog 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
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.