Det finns en funktion, Util_ToWords , i den kostnadsfria versionen av SQL# SQLCLR-biblioteket som jag skrev som gör detta (tja, utan orden "dollar(s)" och "cents" tillagda):
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öljande exempel injicerar orden "dollar(ar)" och "cent" i returvärdet (som ursprungligen var avsett för utskrift på checkar):
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;
Returnerar:
Two dollars and 08 cents