Ada fungsi, Util_ToWords , dalam versi gratis SQL# Pustaka SQLCLR yang saya tulis yang melakukan ini (yah, tanpa kata "dolar(s)" dan "sen" ditambahkan):
SELECT SQL#.Util_ToWords(10); -- Ten
SELECT SQL#.Util_ToWords(100); -- One Hundred
SELECT SQL#.Util_ToWords(1000); -- One Thousand
SELECT SQL#.Util_ToWords(120.20); -- One Hundred Twenty and 20
SELECT SQL#.Util_ToWords(212); -- Two Hundred Twelve
SELECT SQL#.Util_ToWords(123097.4);-- One Hundred Twenty Three Thousand, Ninety Seven and 40
Contoh berikut memasukkan kata "dollar(s)" dan "cents" ke dalam nilai kembalian (yang awalnya dimaksudkan untuk dicetak pada cek):
DECLARE @Amount MONEY = 2.08;
;WITH cte AS
(
SELECT N' dollar' + CASE WHEN @Amount >= 1.00 AND @Amount < 2.00 THEN N''
ELSE N's'
END AS [Currency],
SQL#.Util_ToWords(@Amount) AS [Words]
)
SELECT CASE CHARINDEX(N' and ', cte.[Words])
WHEN 0 THEN cte.[Words] + cte.[Currency]
ELSE STUFF(cte.[Words], CHARINDEX(N' and ', cte.[Words]), 0, cte.[Currency])
+ N' cents'
END
FROM cte;
Pengembalian:
Two dollars and 08 cents