Cara membangun fungsi skalar di SQL server yang mengubah angka menjadi kata dengan ordinal. Contoh: untuk input 25, outputnya harus 'Dua puluh lima'
Terima kasih.
Cara membangun fungsi skalar di SQL server yang mengubah angka menjadi kata dengan ordinal. Contoh: untuk input 25, outputnya harus 'Dua puluh lima'
Terima kasih.
Rentang 0 hingga 9999
declare @t table(col1 int,col2 varchar(50),col3 int)
insert into @t (col1,col2) VALUES (0,'Zero'),(1,'One'),(2,'Two')
,(3,'Three'),(4,'Four'),(5,'Five'),(6,'Six'),(7,'Seven'),(8,'Eight')
,(9,'Nine'),(10,'Ten'),(11,'eleven'),(12,'Twelve'),(13,'thirteen')
,(14,'Fourteen'),(15,'Fifteen'),(16,'Sixteen'),(17,'Seventeen')
,(18,'Eighteen'),(19,'Nineteen'),(20,'Twenty'),(30,'Thirty'),(40,'Forty')
,(50,'Fifty'),(60,'Sixty'),(70,'Seventy'),(80,'Eighty'),(90,'Ninety')
,(100,'Hundreds'),(1000,'Thousands'),(10000,'Thousands')
declare @input varchar(10)='1897'
declare @Words varchar(2000)=''
declare @i int=len(@input)
while((len(@input)>0))
begin
if(@input>=0 and @input<=19 and @i=len(@input))
begin
select @Words=@Words+' '+ col2 from @t where col1 =@input
BREAK
END
else if(len(@input) between 3 and 4)
BEGIN
select @Words=@Words+' '+ col2 from @t where col1 =@input/cast(('1'+replicate('0',len(@input)-1)) as int)
select @Words=@Words+' '+ col2 from @t where col1 ='1'+replicate('0',len(@input)-1)
END
else
begin
select @Words=@Words+' '+ col2 from @t where col1 =left(@input,1)+ replicate('0',@i-1)
END
set @input=stuff(@input,1,1,'')
if(cast(@input as int)=0)
BREAK
set @i=@i-1
end
select @Words