USE [Test532]
GO
CREATE FUNCTION [dbo].[moneyformat]
(@NewAmountBIGINT)
RETURNS Varchar(255)
AS
BEGIN
--DECLAREVariables
DECLARE @StringLenINT
--Findsthe lenghtof the numberthatisusedtodetermine if LoopIterates
DECLARE @I INT
--The counterusedinthe loop
DECLARE @NewAmount1VARCHAR(255)
--Create newvariabletochange parameterintoa VARCHARtouse the substring
function
DECLARE @NextThree VARCHAR(255)
--Variable usedtoreferencethe nextthere digitsafterthe comma
DECLARE @Firstdigitsvarchar(255)
--Variable usedtoholdthe first1 or 2 digitstothe leftof the decimal
DECLARE @RemainderVarchar(255)
--Variable usedtodetermine whichif statementexecutes
DECLARE @ResultVarchar(255)
--Varaiable usedthattotalsthe numbersandcommasfor display
DECLARE @SubPosition INT
--Varaible usedtostore whatpositionneedstobe startedwithduringeachiterationof
the loop
DECLARE @TotalCharINT
--Variable usedtofindthe total lenghtof stringthatgets truncatedby the last character
DECLARE @DecimalDigits INT
-- SET Variables
SET @StringLen= Len(@NewAmount)
SET @NewAmount1=CAST(@NewAmountasVarchar(255))
SET @Remainder= @StringLen%3
SET @FirstDigits=left(@NewAmount1,@Remainder)
SET @SubPosition=@Remainder+1
SET @Result= ISNULL(@Result,'')
SET @I = 0
SET @DecimalDigits=RIGHT(@NewAmount1,2)
WHILE @I < @StringLen
BEGIN
IF @Remainder=0
BEGIN
SET @NextThree =SUBSTRING(@NewAmount1,@SubPosition,3)
SET @Result= @Result+ @NextThree +','
SET @SubPosition=@SubPosition+3
SET @I=@I+3
SET @TotalChar = LEN(@Result)
IF @I = @StringLen
BEGIN
SET @Result=Left(@Result,@TotalChar- 1)
END
END
ELSE
BEGIN
SET @NextThree =SUBSTRING(@NewAmount1,@SubPosition,3)
SET @Result= @Result+ ',' + @NextThree
SET @SubPosition=@SubPosition+3
SET @I=@I+3
SET @TotalChar = LEN(@Result)
IF @I > @StringLen
BEGIN
SET @Result=@FirstDigits+Left(@Result,@TotalChar- 1)
END
END
END
RETURN @Result
END
GO

MoneyFormat.sql

  • 1.
    USE [Test532] GO CREATE FUNCTION[dbo].[moneyformat] (@NewAmountBIGINT) RETURNS Varchar(255) AS BEGIN --DECLAREVariables DECLARE @StringLenINT --Findsthe lenghtof the numberthatisusedtodetermine if LoopIterates DECLARE @I INT --The counterusedinthe loop DECLARE @NewAmount1VARCHAR(255) --Create newvariabletochange parameterintoa VARCHARtouse the substring function DECLARE @NextThree VARCHAR(255) --Variable usedtoreferencethe nextthere digitsafterthe comma DECLARE @Firstdigitsvarchar(255) --Variable usedtoholdthe first1 or 2 digitstothe leftof the decimal DECLARE @RemainderVarchar(255) --Variable usedtodetermine whichif statementexecutes DECLARE @ResultVarchar(255) --Varaiable usedthattotalsthe numbersandcommasfor display DECLARE @SubPosition INT --Varaible usedtostore whatpositionneedstobe startedwithduringeachiterationof the loop DECLARE @TotalCharINT --Variable usedtofindthe total lenghtof stringthatgets truncatedby the last character DECLARE @DecimalDigits INT -- SET Variables SET @StringLen= Len(@NewAmount) SET @NewAmount1=CAST(@NewAmountasVarchar(255)) SET @Remainder= @StringLen%3 SET @FirstDigits=left(@NewAmount1,@Remainder) SET @SubPosition=@Remainder+1 SET @Result= ISNULL(@Result,'') SET @I = 0 SET @DecimalDigits=RIGHT(@NewAmount1,2) WHILE @I < @StringLen BEGIN IF @Remainder=0 BEGIN SET @NextThree =SUBSTRING(@NewAmount1,@SubPosition,3) SET @Result= @Result+ @NextThree +',' SET @SubPosition=@SubPosition+3
  • 2.
    SET @I=@I+3 SET @TotalChar= LEN(@Result) IF @I = @StringLen BEGIN SET @Result=Left(@Result,@TotalChar- 1) END END ELSE BEGIN SET @NextThree =SUBSTRING(@NewAmount1,@SubPosition,3) SET @Result= @Result+ ',' + @NextThree SET @SubPosition=@SubPosition+3 SET @I=@I+3 SET @TotalChar = LEN(@Result) IF @I > @StringLen BEGIN SET @Result=@FirstDigits+Left(@Result,@TotalChar- 1) END END END RETURN @Result END GO