USE [GrpInsurance]
GO

/****** Object:  UserDefinedFunction [dbo].[func_curr_format_ina]    Script Date: 05/04/2018 17:38:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE FUNCTION [dbo].[func_curr_format_ina](@Nilai float, @Digit int, @Dec int, @Sp bit)
RETURNS Varchar(100)
BEGIN 
     DECLARE @Hasil Varchar(100)
     DECLARE @decimal Varchar(10)
     DECLARE @bulat Varchar(100)
     DECLARE @Titik int
     DECLARE @loop int
     SET @Hasil=LTRIM(RTRIM(STR(@Nilai, @Digit, @Dec)))
     
     SET @titik=CHARINDEX('.',@Hasil)

     IF @Dec<>0
     SET @decimal =SUBSTRING(@Hasil, @Titik+1, @Dec)
     ELSE SET @decimal =@Hasil

     SET @bulat =SUBSTRING(@Hasil, 1, @Titik-1)
     IF @sp=1
     BEGIN  
     SET @Hasil=''
     SET @Loop=0
     WHILE @Loop<LEN(@Bulat)
     BEGIN
          IF (@Loop % 3 =0) AND (@Loop<>0) SET @Hasil= ','+@Hasil
          SET @Hasil= SubString(@Bulat,LEN(@Bulat)-@Loop,1)+@Hasil 
          SET @Loop = @Loop +1
     END
     SET @Hasil= REPLACE(@Hasil,',','.') +','+@Decimal
     END
     Else SET @Hasil= REPLACE(@Bulat ,',','.') +','+@Decimal 
     Return(@hasil)
END


GO


