dbo.f_amount_in_word

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Null on Null Input

False

Schema Bound

False

Assembly

Parameters

Name Data Type Length Description
(Result) varchar 250
@the_amount money 8

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE FUNCTION dbo.f_amount_in_word (@the_amount money) 
RETURNS varchar(250)
WITH EXECUTE AS CALLER

AS
BEGIN
 
DECLARE @divisor bigint,
 @large_amount money,
 @tiny_amount money,
 @dividen money,
 @dummy  money,
 @the_word varchar(250),
 @weight varchar(100),
 @unit  varchar(30),
 @follower varchar(50),
 @prefix varchar(10),
 @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
 RETURN 'OUT OF RANGE' 

WHILE @divisor >= 1
BEGIN 
 SET @dividen = FLOOR(@large_amount / @divisor)
 
 SET @large_amount = CONVERT(bigint,@large_amount) % @divisor
 
 SET @unit = ''
 IF @dividen > 0.00
  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 )

 SET @weight = '' 
 SET @dummy = @dividen
 IF @dummy >= 100.00
  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 '

 SET @dummy = CONVERT(bigint,@dividen) % 100

 IF @dummy < 10.00
 BEGIN
  IF @dummy = 1.00 AND @unit = 'RIBU'
  BEGIN
   IF @dividen=@dummy
    SET @weight = @weight + 'SE'
   ELSE
    SET @weight = @weight + 'SATU '
  END
  ELSE
  IF @dummy > 0.00 
   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)
 END
 ELSE
 IF @dummy BETWEEN 11 AND 19
  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
 BEGIN
  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 
   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 )
 END
 
 SET @the_word = @the_word + @weight + @unit
 SET @divisor = @divisor / 1000.00
END

IF FLOOR(@the_amount) = 0.00 
 SET @the_word = 'NOL '

SET @follower = ''
IF @tiny_amount < 10.00
BEGIN 
 IF @tiny_amount > 0.00 
  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)
END
ELSE
BEGIN
 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 
  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)
END 
 
SET @the_word = @the_word + @follower

IF @the_amount < 0.00
 SET @the_word = 'MINUS ' + @the_word
 
RETURN @the_word
END




GO

Depends On

No items found

Used By

No items found