In SQL Server 2005, 2008 or 2012, it is simple to convert a number into Western Currency Format, but if to convert it into Indian Currency Format, there is no direct Inbuilt Function available in MS SQL. To convert it into Indian Format you need to write an SQL script. And to Convert in Hindi words, follow the link here.. . And if you wish to Convert in English words,then follow the link here..
Below is the MS SQL User Defined Function which converts Number into Western Currency Format and in Indian Currency Format as well :
CREATE FUNCTION [dbo].[udf_NumberToCurrency](@InNumericValue NUMERIC(38,2),@InFormatType VARCHAR(10))RETURNS VARCHAR(60)ASBEGINDECLARE @RetVal VARCHAR(60),@StrRight VARCHAR(5),@StrFinal VARCHAR(60),@StrLength INTSET @RetVal = ''SET @RetVal= @InNumericValueSET @RetVal= SUBSTRING(@RetVal,1,CASE WHEN CHARINDEX('.', @RetVal)=0 THEN LEN(@RetVal)ELSE CHARINDEX('.',@RetVal)-1 END)
IF(@InFormatType = 'US')BEGINSET @StrFinal= CONVERT(VARCHAR(60), CONVERT(MONEY, @RetVal) , 1)SET @StrFinal= SUBSTRING(@StrFinal,0,CHARINDEX('.', @StrFinal))END
ELSEIF(@InFormatType = 'IND')BEGINSET @StrLength = LEN(@RetVal)IF(@StrLength > 3)BEGINSET @StrFinal = RIGHT(@RetVal,3)SET @RetVal = SUBSTRING(@RetVal,-2,@StrLength)SET @StrLength = LEN(@RetVal)IF (LEN(@RetVal) > 0 AND LEN(@RetVal) < 3)BEGINSET @StrFinal = @RetVal + ',' + @StrFinalENDWHILE LEN(@RetVal) > 2BEGINSET @StrRight=RIGHT(@RetVal,2)SET @StrFinal = @StrRight + ',' + @StrFinalSET @RetVal = SUBSTRING(@RetVal,-1,@StrLength)SET @StrLength = LEN(@RetVal)IF(LEN(@RetVal) > 2)CONTINUEELSESET @StrFinal = @RetVal + ',' + @StrFinalBREAKENDENDELSEBEGINSET @StrFinal = @RetValENDENDSELECT @StrFinal = ISNULL(@StrFinal,00)RETURN @StrFinalEND
Above function takes two parameters as input, firstone is the number you want to format and second one is the format type ('US' for Western and 'IND' for Indian Formats). and in return give a formatted string.
e.g.SELECT dbo.udf_NumberToCurrency (1116548238.53,'US') AS [Amount]--Output :[Amount]1,116,548,238SELECT dbo.udf_NumberToCurrency (1116548238.53,'IND') AS [Amount]--Output :[Amount]1,11,65,48,238
This is to be
noted here that, the decimal points will be omitted in both formats. But if you like to have the decimal points as well, then make a bit change in the above function as below :
CREATE FUNCTION [dbo].[udf_NumberToCurrency](@InNumericValue NUMERIC(38,2),@InFormatType VARCHAR(10))RETURNS VARCHAR(60)ASBEGINDECLARE @RetVal VARCHAR(60),@StrRight VARCHAR(5),@StrFinal VARCHAR(60),@StrLength INTSET @RetVal = ''SET @RetVal= @InNumericValue
IF(@InFormatType = 'US')BEGINSET @StrFinal= CONVERT(VARCHAR(60), CONVERT(MONEY, @RetVal) , 1)END
ELSEIF(@InFormatType = 'IND')BEGINSET @StrLength = LEN(@RetVal)IF(@StrLength > 6)BEGINSET @StrFinal = RIGHT(@RetVal,6)SET @RetVal = SUBSTRING(@RetVal,-5,@StrLength)SET @StrLength = LEN(@RetVal)IF (LEN(@RetVal) > 0 AND LEN(@RetVal) < 3)BEGINSET @StrFinal = @RetVal + ',' + @StrFinalENDWHILE LEN(@RetVal) > 2BEGINSET @StrRight=RIGHT(@RetVal,2)SET @StrFinal = @StrRight + ',' + @StrFinalSET @RetVal = SUBSTRING(@RetVal,-1,@StrLength)SET @StrLength = LEN(@RetVal)IF(LEN(@RetVal) > 2)CONTINUEELSESET @StrFinal = @RetVal + ',' + @StrFinalBREAKENDENDELSEBEGINSET @StrFinal = @RetValENDENDSELECT @StrFinal = ISNULL(@StrFinal,00)RETURN @StrFinalEND
And now try for your inputs :
SELECT dbo.udf_NumberToCurrency (1116548238.53,'US') AS [Amount]--Output :[Amount]1,116,548,238.53SELECT dbo.udf_NumberToCurrency (1116548238.53,'IND') AS [Amount]--Output :[Amount]1,11,65,48,238.53
Reference: Govind Badkur(http://sqlserver20.blogspot.com)
Thank you very much this blog was very helpful...for me.
ReplyDeleteThanks A TO N
ReplyDeleteThank you very much sir....
ReplyDeleteHow to allow 4 decimal point
ReplyDeletevery help full.. appreciated!!!
ReplyDeletewhile entering -ve amount it is not working
ReplyDeleteI recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. convert money
ReplyDeletenice
ReplyDeleteelazığ
ReplyDeletevan
mardin
sakarya
düzce
KSH
bingöl
ReplyDeleteelazığ
hakkari
sakarya
erzincan
76MQHC
elazığ
ReplyDeletegümüşhane
kilis
siirt
sakarya
1S46
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
33R
antalya evden eve nakliyat
ReplyDeleteankara evden eve nakliyat
bursa evden eve nakliyat
yalova evden eve nakliyat
gümüşhane evden eve nakliyat
V0U8GM
9B360
ReplyDeleteTekirdağ Evden Eve Nakliyat
Bartın Lojistik
Aydın Lojistik
Antep Evden Eve Nakliyat
Afyon Evden Eve Nakliyat
97DF7
ReplyDeleteBatman Evden Eve Nakliyat
Şırnak Parça Eşya Taşıma
Batman Parça Eşya Taşıma
Artvin Evden Eve Nakliyat
Kütahya Lojistik
4218A
ReplyDeleteGiresun Evden Eve Nakliyat
Uşak Evden Eve Nakliyat
Çorum Lojistik
Karabük Lojistik
Mardin Parça Eşya Taşıma
70378
ReplyDeleteDüzce Lojistik
Ardahan Parça Eşya Taşıma
Mersin Parça Eşya Taşıma
Batman Parça Eşya Taşıma
Denizli Lojistik
E71E5
ReplyDeleteArdahan Lojistik
Uşak Lojistik
Denizli Lojistik
Erzincan Lojistik
Urfa Lojistik
2CAA6
ReplyDeleteMuş Şehir İçi Nakliyat
Ankara Lojistik
Niğde Parça Eşya Taşıma
Tokat Parça Eşya Taşıma
Siirt Lojistik
Denizli Şehirler Arası Nakliyat
Zonguldak Evden Eve Nakliyat
Maraş Lojistik
Ünye Oto Boya
5D4F4
ReplyDeleteTrabzon Şehir İçi Nakliyat
Çerkezköy Organizasyon
Aksaray Lojistik
Siirt Şehirler Arası Nakliyat
Gate io Güvenilir mi
Uşak Parça Eşya Taşıma
Silivri Cam Balkon
Maraş Evden Eve Nakliyat
Maraş Şehir İçi Nakliyat
0E395
ReplyDeleteaave
raydium
avax
trezor suite
pancakeswap
poocoin
trust wallet
defilama
galagames
FDHNYNMKMI
ReplyDeleteتسليك مجاري بالقطيف
شركة صيانة افران بجدة PV1cVLAiip
ReplyDeleteشركة تنظيف خزانات oIJD3puelZ
ReplyDeleteشركة تنظيف مجالس بالاحساء 14pLIUpfRM
ReplyDelete