Der er en funktion, Util_ToWords , i den gratis version af SQL# SQLCLR-bibliotek, som jeg skrev, der gør dette (godt, uden ordene "dollar(e)" og "cents" tilføjet):
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
Følgende eksempel injicerer ordene "dollar(e)" og "cents" i returværdien (som oprindeligt var beregnet til udskrivning på checks):
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;
Returnerer:
Two dollars and 08 cents