La vida de un DBA, Basis y Bedel en Puerto Rico
2010/02/21
  SQL Server left pad with zeros function

Funcion en SQL Server para ajustar un string a la derecha, llenandolo a la izquierda con ceros.

if object_id ( 'fnLeftPadWithZeros', 'FN' ) is not null
   drop function fnLeftPadWithZeros
go

create function fnLeftPadWithZeros(@strInput varchar(max), @IntLenght smallint)
   returns varchar(max)
as
begin
   if @strInput is not null
   begin
      if len(@strInput) <= @IntLenght
      begin
         set @strInput = replicate('0', @IntLenght - len(ltrim(rtrim(@strInput)))) +
                         @strInput
      end
   end
   return @strInput
end
go

-- PRUEBA DE LA FUNCION
declare @Text varchar(256)
set @Text = '1'
select Original = @Text
select SpacesRemoved = dbo.fnLeftPadWithZeros(@Text, 10)
go

--

 
Comentarios: Publicar un comentario

Suscribirse a Comentarios de la entrada [Atom]





<< Página Principal

Mi foto
Nombre: MADS
Ubicación: San Juan, Puerto Rico, United States
Archivos
mayo 2007 / febrero 2008 / marzo 2008 / abril 2008 / mayo 2008 / junio 2008 / julio 2008 / febrero 2010 / marzo 2010 /


Powered by Blogger

Suscribirse a
Entradas [Atom]