Link to home
Start Free TrialLog in
Avatar of thcit
thcitFlag for United States of America

asked on

Padding Function

Hello all,

Kind of new at this SQL stuff and I'm needing to pad some values when I run a query.  Does anyone know of a good way to pad values in a query via a function or something?  Also I need the pad to be with 0's with my data right justified. (0's on the left)

Thanks,
BBS
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

e.g.
To pad a value to 4 digits:

DECLARE @intNumber INTEGER
SET @intNumber = 1

SELECT RIGHT('0000' + CAST(@intNumber AS VARCHAR), 4)
ASKER CERTIFIED SOLUTION
Avatar of rob_lorentz
rob_lorentz

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 Brian Crowe
CREATE FUNCTION PadLeft(@text varchar(1000), @fillchar char(1), @totallength int)
RETURNS varchar(1000)
AS
WHILE LEN(@text) < @totallength
     SET @text = @fillchar + @text
RETURN @text
END
Avatar of thcit

ASKER

Thanks Rob, that was exactly what I needed.  Functional and lightweight.