Friday, March 5, 2010

Convert a Money to Indian Currency Format in sql server

I searched in google an found nothing on converting money to indian currency format, there were some codes to convert money to U.S currency format. So i decided to write one for my own.

here is the function


CREATE FUNCTION dbo.udf_MoneyFormat(@amount MONEY)
RETURNS VARCHAR(50)
AS
/* --Comments--Created By: Alwyn Duraisingh.M
Created On: 2nd March 2010
Purpose: To convert a Money to Indian Currency Format
*/
BEGIN
DECLARE @charMoney VARCHAR(50)
,@RemainingChar VARCHAR(50)
,QuotChar VARCHAR(50)
DECLARE @LenStr INT, @val INT, @index INT
SELECT @charMoney = CONVERT(VARCHAR(50),@amount)
SELECT @QuotChar = SUBSTRING(@charMoney,1,CHARINDEX('.',@charMoney)-1) SELECT @val = LEN(@charMoney) - LEN(@QuotChar)
SELECT @RemainingChar = SUBSTRING(@charMoney,CHARINDEX('.',@charMoney),@val)
SELECT @LenStr = LEN(@QuotChar)
SET @index = 3
WHILE (@LenStr > @index)
BEGIN
SET @QuotChar = (SELECT STUFF(@QuotChar, (@LenStr-@index) + 1, 0, ','))
SET @index = @index + 2
END
RETURN @QuotChar + @RemainingCharEND

You can call this function like;


DECLARE @amount Money
SET @amount = 1000023.08
SELECT dbo.udf_MoneyFormat(@amount)

RESULT:
-------

10,00,023.08

No comments:

Post a Comment