CREATE FUNCTION dbo.MoneySpeller (in the_amount money)
RETURNS varchar(250)
BEGIN
DECLARE divisor bigint;
DECLARE large_amount money;
DECLARE tiny_amount money;
DECLARE dividen money;
DECLARE dummy money;
DECLARE the_word varchar(250);
DECLARE weight varchar(100);
DECLARE unit varchar(30);
DECLARE follower varchar(50);
DECLARE prefix varchar(10);
DECLARE sufix varchar(10);
--SET NOCOUNT ON
SET the_word = '';
SET large_amount = FLOOR(ABS(the_amount) );
SET tiny_amount = ROUND((ABS(the_amount) - large_amount ) * 100.00,0);
SET divisor = 1000000000000.00;
IF large_amount > divisor * 1000.00 then
RETURN 'OUT OF RANGE' endif;
WHILE divisor >= 1
LOOP
SET dividen = FLOOR(large_amount / divisor);
SET large_amount = CONVERT(bigint,large_amount) % divisor;
SET unit = '';
IF dividen > 0.00 then
SET unit=(CASE divisor
WHEN 1000000000000.00 THEN 'TRILYUN '
WHEN 1000000000.00 THEN 'MILYAR '
WHEN 1000000.00 THEN 'JUTA '
WHEN 1000.00 THEN 'RIBU '
ELSE unit
END ) endif;
SET weight = '' ;
SET dummy = dividen;
IF dummy >= 100.00 then
SET weight = (CASE FLOOR(dummy / 100.00)
WHEN 1 THEN 'SE'
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END ) + 'RATUS ' endif;
SET dummy = CONVERT(bigint,dividen) % 100;
IF dummy < 10.00 then
IF dummy = 1.00 AND unit = 'RIBU' then
IF dividen=dummy then
SET weight = weight + 'SE';
ELSE
SET weight = weight + 'SATU ' endif;
ELSE
IF dummy > 0.00 then
SET weight = weight + (CASE dummy
WHEN 1 THEN 'SATU '
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END) endif endif;
ELSE
IF dummy BETWEEN 11 AND 19 then
SET weight = weight + (CASE CONVERT(bigint,dummy) % 10
WHEN 1 THEN 'SE'
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END ) + 'BELAS ';
ELSE
SET weight = weight + (CASE FLOOR(dummy / 10)
WHEN 1 THEN 'SE'
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END ) + 'PULUH ';
IF CONVERT(bigint,dummy) % 10 > 0 then
SET weight = weight + (CASE CONVERT(bigint,dummy) % 10
WHEN 1 THEN 'SATU '
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END ) endif;
ENDif endif;
SET the_word = the_word + weight + unit;
SET divisor = divisor / 1000.00;
END loop;
IF FLOOR(the_amount) = 0.00 then
SET the_word = 'NOL ' endif;
SET follower = '';
IF tiny_amount < 10.00
then
IF tiny_amount > 0.00 then
SET follower = 'KOMA NOL ' + (CASE tiny_amount
WHEN 1 THEN 'SATU '
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END) endif;
else
SET follower = 'KOMA ' + (CASE FLOOR(tiny_amount / 10.00)
WHEN 1 THEN 'SATU '
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END);
IF CONVERT(bigint,tiny_amount) % 10 > 0 then
SET follower = follower + (CASE CONVERT(bigint,tiny_amount) % 10
WHEN 1 THEN 'SATU '
WHEN 2 THEN 'DUA '
WHEN 3 THEN 'TIGA '
WHEN 4 THEN 'EMPAT '
WHEN 5 THEN 'LIMA '
WHEN 6 THEN 'ENAM '
WHEN 7 THEN 'TUJUH '
WHEN 8 THEN 'DELAPAN '
ELSE 'SEMBILAN ' END) endif;
endif;
SET the_word = the_word + follower;
IF the_amount < 0.00 then
SET the_word = 'MINUS ' + the_word endif;
RETURN trim(the_word)
END
Contoh pemanggilan
SELECT dbo.MoneySpeller(12512512312)
Hasilnya
DUA BELAS RIBU EMPAT RATUS DUA BELAS