thcit
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
RETURNS varchar(1000)
AS
WHILE LEN(@text) < @totallength
SET @text = @fillchar + @text
RETURN @text
END
ASKER
Thanks Rob, that was exactly what I needed. Functional and lightweight.
To pad a value to 4 digits:
DECLARE @intNumber INTEGER
SET @intNumber = 1
SELECT RIGHT('0000' + CAST(@intNumber AS VARCHAR), 4)