Skip to content

Instantly share code, notes, and snippets.

@luchosrock
Last active March 1, 2016 12:54
Show Gist options
  • Save luchosrock/963faed7c513102813f7 to your computer and use it in GitHub Desktop.
Save luchosrock/963faed7c513102813f7 to your computer and use it in GitHub Desktop.
Formatear monto decimal a money con 4 decimales con separador de miles (.) y separador de decimales (,)
CREATE FUNCTION FormatearMonto(@NumStr varchar(50))
RETURNS Varchar(50)
AS
BEGIN
declare @OutStr varchar(50)
declare @i int
declare @run int
Select @i=CHARINDEX('.',@NumStr)
if @i=0
begin
set @i=LEN(@NumStr)
Set @Outstr=''
end
else
begin
Set @Outstr=SUBSTRING(@NUmStr,@i,50)
Set @i=@i -1
end
Set @run=0
While @i>0
begin
if @Run=3
begin
Set @Outstr=','+@Outstr
Set @run=0
end
Set @Outstr=SUBSTRING(@NumStr,@i,1) +@Outstr
Set @i=@i-1
Set @run=@run + 1
end
RETURN replace(replace(replace(replace(@OutStr,',','*'),'.','x'),'*','.'),'x',',')
--RETURN @OutStr
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment