CopyCode The Code is as follows: Alter function udf_util_convertcurrencytoenglish
(
@ Money numeric (15,2 ),
@ Unit varchar (10) = 'baht'
) Returns varchar (400)
As
/*
/// <Summary>
/// Convert money to English
/// </Summary>
/// <Param name = "@ money"> e.g. 1234.56 </param>
/// <Param name = "@ unit"> e.g. 'baht '</param>
/// <Returns> English money </returns>
*/
Begin
Declare @ result varchar (400)
If @ money = 0
Set @ result = 'zero '+ @ Unit
Else
Begin
Declare @ I int, @ hundreds int, @ tenth int, @ one int, @ thousand int, @ million int, @ billion int, @ numbers varchar (400 ), @ s varchar (15)
Set @ numbers = 'one two three four five'
+ 'Six seven eight nine ten'
+ 'Even tweleve thirteen fourteen effecteen'
+ 'Sixteen Seventeen eighteen nineteen'
+ 'Twenty thirty forty equalty'
+ 'Sixty seventy eighty ninety'
Set @ s = right ('000000' + Cast (@ money as varchar (15), 15)
Set @ billion = cast (substring (@ s, 1, 3) as INT)
Set @ million = cast (substring (@ s, 4, 3) as INT)
Set @ thousand = cast (substring (@ s, 7,3) as INT)
Set @ result =''
Set @ I = 0
While @ I <= 3
Begin
Set @ hundreds = cast (substring (@ s, @ I * 3 + 1, 1) as INT)
Set @ tenth = cast (substring (@ s, @ I * 3 + 2, 1) as INT)
Set @ one = (Case @ tenth when 1 then 10 else 0 end) + Cast (substring (@ s, @ I * 3 + 3, 1) as INT)
Set @ tenth = (case when @ tenth <= 1 then 0 else @ tenth end)
If (@ I = 3 and (@ billion> 0 or @ million> 0 or @ thousand> 0) and (@ hundreds = 0 and (@ tenth> 0 or @ one> 0 )))
Set @ result = @ result + 'and'
If @ hundreds> 0
Set @ result = @ result + rtrim (substring (@ numbers, @ hundreds * 10-9, 10) + 'hundred'
If @ tenth> = 2 and @ tenth <= 9
Begin
If @ hundreds> 0
Set @ result = @ result + 'and'
Set @ result = @ result + rtrim (substring (@ numbers, @ tenth * 10 + 171,10) +''
End
If @ one> = 1 and @ one <= 19
Begin
If @ hundreds> 0 and @ tenth = 0
Set @ result = @ result + 'and'
Set @ result = @ result + rtrim (substring (@ numbers, @ one * 10-9, 10 ))
End
If @ I = 0 and @ billion> 0
Set @ result = @ result + 'billion'
If @ I = 1 and @ million> 0
Set @ result = @ result + 'million'
If @ I = 2 and @ thousand> 0
Set @ result = @ result + 'thousand'
Set @ I = @ I + 1
End
If (@ result <> '')
Set @ result = @ result + ''+ @ Unit
If substring (@ s, 14,2) <> '00'
Begin
Set @ tenth = cast (substring (@ s, 14,1) as INT)
Set @ one = cast (substring (@ s, 15,1) as INT)
If (@ tenth> = 2 and @ tenth <= 9)
Set @ result = @ result + rtrim (substring (@ numbers, @ tenth * 10 + 171,10 ))
If @ tenth = 1 and @ one> = 1 and @ one <= 19
Set @ result = @ result + ''+ rtrim (substring (@ numbers, cast (substring (@ s, 14,2) as INT) * 10-9,10 ))
Else
Set @ result = @ result + ''+ rtrim (substring (@ numbers, @ one * 10-9, 10 ))
Set @ result = @ result + 'satang'
End
Else
Set @ result = @ result + 'only'
End
Return @ result
End