Website Design United States, Website Design California, Website Designing United States, Website Designing California

PadLeft and PadRight - SQL

Josh Fruits

http://www.programmers-corner.com

josh@programmerscorner.com

These functions are used to pad the left or right side of your text with spaces. This is useful when outputting fixed width data.

create function PadLeft (@value varchar(1024), @FieldLength int)
returns varchar(1024)
AS
BEGIN
declare @ret varchar(1024)
if @FieldLength > 0
set @ret = right(space(@FieldLength) + @value, @FieldLength)
else
set @ret = @value
return(@ret)
END

create function PadRight (@value varchar(1024), @FieldLength int)
returns varchar(1024)
AS
BEGIN
declare @ret varchar(1024)
if @FieldLength > 0
set @ret = left(@value + space(@FieldLength), @FieldLength)
else
set @ret = @value
return(@ret)
END

 















 


© 2008-2009 dotnet4all.com