Technical Article

Text to Speech + Convert Numbers to Words in 4 languages

,

These are functions and stored procedures to convert numbers to words, in three languages: English, Portuguese and Spanish. There is also a function to convert text to speech.

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE function NúmerosPalabras(@s VARCHAR(50)) 
--Spanish words for numbers
returns VARCHAR(1024)
as
BEGIN
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='cero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
SET @result=case 
WHEN (@i-1=3)  AND (@c+@b+@a<>'000') THEN 'mil'
WHEN (@i-1=6)  AND (@c+@b+@a<>'000')  AND(@orlen=7)AND(@a='1')   THEN 'millón '
WHEN (@i-1=6)  AND (@c+@b+@a<>'000') THEN 'millones '
WHEN (@i-1=9)  AND (@c+@b+@a<>'000')  THEN 'mil millones'
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  AND (@orlen=13)AND(@a='1')   THEN 'billón '
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')   THEN 'billones '
WHEN (@i-1=15)  AND (@c+@b+@a<>'000')  THEN 'mil billones'
WHEN (@i-1=18)  AND (@c+@b+@a<>'000')  AND (@orlen=19)AND(@a='1')   THEN 'trillón '
WHEN (@i-1=18)   AND (@c+@b+@a<>'000')  THEN 'trillones '
WHEN (@i-1=21)  AND (@c+@b+@a<>'000')  THEN 'mil trillones'
WHEN (@i-1=24)  AND (@c+@b+@a<>'000') AND (@orlen=25)AND(@a='1')   THEN 'quadrillón '
WHEN  (@i-1=24)  AND (@c+@b+@a<>'000')   THEN 'quadrillones '
WHEN (@i-1=27)  AND (@c+@b+@a<>'000')  THEN 'mil quadrillones'
WHEN (@i-1=30)  AND (@c+@b+@a<>'000')  AND (@orlen=31)AND(@a='1')   THEN 'quintillón '
WHEN  (@i-1=30)  AND (@c+@b+@a<>'000')  THEN 'quintillones '
WHEN (@i-1=33)  AND (@c+@b+@a<>'000')  THEN 'mil quintillones'
WHEN (@i-1=36)  AND (@c+@b+@a<>'000') AND (@orlen=37)AND(@a='1')   THEN 'sextillón '
WHEN  (@i-1=36)  AND (@c+@b+@a<>'000')  THEN 'sextillones '
WHEN (@i-1=39)  AND (@c+@b+@a<>'000')  THEN 'mil sextillones'
WHEN (@i-1=42)  AND (@c+@b+@a<>'000')  AND (@orlen=43)AND(@a='1')   THEN 'septillón '
WHEN  (@i-1=42)  AND (@c+@b+@a<>'000')  THEN 'septillones '
WHEN (@i-1=45)  AND (@c+@b+@a<>'000')  THEN 'mil septillones'
WHEN (@i-1=48)  AND (@c+@b+@a<>'000') AND (@orlen=49)AND(@a='1')   THEN 'octillón '
WHEN  (@i-1=48)   AND (@c+@b+@a<>'000')  THEN 'octillones '
WHEN (@i-1=51)  AND (@c+@b+@a<>'000')   THEN 'mil octillones'
WHEN (@i-1=54)  AND (@c+@b+@a<>'000')  AND (@orlen=55)AND(@a='1')   THEN 'nonillón '
WHEN  (@i-1=54)  AND (@c+@b+@a<>'000')  THEN 'nonillones '
WHEN (@i-1=57) THEN 'mil nonillones'
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'un'
WHEN '2' THEN 'dos'
WHEN '3' THEN 'tres'
WHEN '4' THEN 'cuatro'
WHEN '5' THEN 'cinco'
WHEN '6' THEN 'seis'
WHEN '7' THEN 'siete'
WHEN '8' THEN 'ocho'
WHEN '9' THEN 'nueve'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1' OR @b='2' 
SET @result=case @b+@a 
WHEN '10' THEN 'diez'
WHEN '11' THEN 'once'
WHEN '12' THEN 'doce'
WHEN '13' THEN 'trece'
WHEN '14' THEN 'catorce'
WHEN '15' THEN 'quince'
WHEN '16' THEN 'dieciséis'
WHEN '17' THEN 'diecisiete'
WHEN '18' THEN 'dieciocho'
WHEN '19' THEN 'diecinueve'
WHEN '20' THEN 'veinte'
WHEN '21' THEN 'veintiuno'
WHEN '22' THEN 'veintidós'
WHEN '23' THEN 'veintitrés'
WHEN '24' THEN 'veinticuatro'
WHEN '25' THEN 'veinticinco'
WHEN '26' THEN 'veintiséis'
WHEN '27' THEN 'veintisiete'

WHEN '28' THEN 'veintiocho'
WHEN '29' THEN 'veintinueve'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 

WHEN '3' THEN 'treinta'
WHEN '4' THEN 'cuarenta'
WHEN '5' THEN 'cincuenta'
WHEN '6' THEN 'sesenta'
WHEN '7' THEN 'setenta'
WHEN '8' THEN 'ochenta'
WHEN '9' THEN 'noventa'
ELSE ''
END+case @a WHEN '0' THEN '' ELSE ' y' END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
IF @a='0'  AND @b='0' 
SET @result=case @c 
WHEN '1' THEN 'cien'
WHEN '2' THEN 'doscientos'
WHEN '3' THEN 'trescientos'
WHEN '4' THEN 'cuatrocientos'
WHEN '5' THEN 'quinientos'
WHEN '6' THEN 'seiscientos'
WHEN '7' THEN 'setecientos'
WHEN '8' THEN 'ochocientos'
WHEN '9' THEN 'novecientos'
ELSE ''
END+' '+@result
ELSE
SET @result=case @c 
WHEN '1' THEN 'ciento'
WHEN '2' THEN 'doscientos'
WHEN '3' THEN 'trescientos'
WHEN '4' THEN 'cuatrocientos'
WHEN '5' THEN 'quinientos'
WHEN '6' THEN 'seiscientos'
WHEN '7' THEN 'setecientos'
WHEN '8' THEN 'ochocientos'
WHEN '9' THEN 'novecientos'
ELSE ''
END+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
IF left(@result,7)='un mil '
SET @result=right(@result,LEN(@result)-3)
IF right(@result,3)=' un'
SET @result=@result+'o'
IF left(@result,2)='y '
SET @result=right(@result,LEN(@result)-2)
While CHARINDEX('  ', @result)>0
SET @result=REPLACE(@result,'  ',' ')
IF dbo.INCLUDED('millones', @result)>1
SET @result=REPLACE(@result,'mil millones','mil')
IF dbo.INCLUDED('billones', @result)>1
SET @result=REPLACE(@result,'mil billones','mil')
IF dbo.INCLUDED('trillones', @result)>1
SET @result=REPLACE(@result,'mil trillones','mil')
IF dbo.INCLUDED('quadrillones', @result)>1
SET @result=REPLACE(@result,'mil quadrillones','mil')
IF dbo.INCLUDED('quintillones', @result)>1
SET @result=REPLACE(@result,'mil quintillones','mil')
IF dbo.INCLUDED('sextillones', @result)>1
SET @result=REPLACE(@result,'mil sextillones','mil')
IF dbo.INCLUDED('septillones', @result)>1
SET @result=REPLACE(@result,'mil septillones','mil')
IF dbo.INCLUDED('octillones', @result)>1
SET @result=REPLACE(@result,'mil octillones','mil')
IF dbo.INCLUDED('nonillones', @result)>1
SET @result=REPLACE(@result,'mil nonillones','mil')
return @result
END








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE function NumbersWords(@s VARCHAR(50)) 
--English words for numbers
returns VARCHAR(1024)
as
BEGIN
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='zero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
SET @result=case 
WHEN (@i-1=3) AND (@c+@b+@a<>'000') THEN 'thousand'
WHEN (@i-1=6) AND (@c+@b+@a<>'000')  THEN 'million'
WHEN (@i-1=9) AND (@c+@b+@a<>'000')  THEN 'billion'
WHEN (@i-1=12) AND (@c+@b+@a<>'000')  THEN 'trillion'
WHEN (@i-1=15) AND (@c+@b+@a<>'000')  THEN 'quadrillion'
WHEN (@i-1=18) AND (@c+@b+@a<>'000')  THEN 'quintillion'
WHEN (@i-1=21) AND (@c+@b+@a<>'000')  THEN 'sextillion'
WHEN (@i-1=24) AND (@c+@b+@a<>'000')  THEN 'septillion'
WHEN (@i-1=27) AND (@c+@b+@a<>'000')  THEN 'octillion'
WHEN (@i-1=30)  AND (@c+@b+@a<>'000') THEN 'nonillion'
WHEN (@i-1=33) AND (@c+@b+@a<>'000')  THEN 'decillion'
WHEN (@i-1=36) AND (@c+@b+@a<>'000')  THEN 'undecillion'
WHEN (@i-1=39) AND (@c+@b+@a<>'000')  THEN 'duodecillion'
WHEN (@i-1=42) AND (@c+@b+@a<>'000')  THEN 'tredecillion'
WHEN (@i-1=45) AND (@c+@b+@a<>'000')   THEN 'quattuordecillion'
WHEN (@i-1=48) AND (@c+@b+@a<>'000')   THEN 'quindecillion'
WHEN (@i-1=51) AND (@c+@b+@a<>'000')  THEN 'sexdecillion'
WHEN (@i-1=54) AND (@c+@b+@a<>'000')  THEN 'septendecillion'
WHEN (@i-1=57) AND (@c+@b+@a<>'000')   THEN 'octodecillion'
WHEN (@i-1=60) THEN 'novemdecillion'
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'one'
WHEN '2' THEN 'two'
WHEN '3' THEN 'three'
WHEN '4' THEN 'four'
WHEN '5' THEN 'five'
WHEN '6' THEN 'six'
WHEN '7' THEN 'seven'
WHEN '8' THEN 'eight'
WHEN '9' THEN 'nine'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1'
SET @result=case @a 
WHEN '0' THEN 'ten'
WHEN '1' THEN 'eleven'
WHEN '2' THEN 'twelve'
WHEN '3' THEN 'thirteen'
WHEN '4' THEN 'fourteen'
WHEN '5' THEN 'fifteen'
WHEN '6' THEN 'sixteen'
WHEN '7' THEN 'seventeen'
WHEN '8' THEN 'eighteen'
WHEN '9' THEN 'nineteen'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 
WHEN '2' THEN 'twenty'
WHEN '3' THEN 'thirty'
WHEN '4' THEN 'fourty'
WHEN '5' THEN 'fifty'
WHEN '6' THEN 'sixty'
WHEN '7' THEN 'seventy'
WHEN '8' THEN 'eighty'
WHEN '9' THEN 'ninety'
ELSE ''
END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
SET @result=case @c 
WHEN '1' THEN 'one'
WHEN '2' THEN 'two'
WHEN '3' THEN 'three'
WHEN '4' THEN 'four'
WHEN '5' THEN 'five'
WHEN '6' THEN 'six'
WHEN '7' THEN 'seven'
WHEN '8' THEN 'eight'
WHEN '9' THEN 'nine'
ELSE ''
END+' hundred'+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
return @result
END









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE function INCLUDED(@s varchar(255),@p varchar(255) ) 
--Returns how many times a string is included (occurs) into another one.
returns int
as
BEGIN
DECLARE @i int,@c int
SET @i=1
SET @c=0
WHILE charindex(@s, @p, @i)>0
BEGIN
SET @i=charindex(@s, @p, @i)+1
SET @c=@c+1
END
RETURN  @c
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE function NúmerosPalavras(@s VARCHAR(50)) 
--Portuguese words for numbers
returns VARCHAR(1024)
as
BEGIN
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='zero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
SET @result=case 
WHEN (@i-1=3)  AND (@c+@b+@a<>'000') THEN 'mil'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=6)  AND (@c+@b+@a<>'000')  AND(@orlen=7)AND(@a='1')   THEN 'milhão '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=6)  AND (@c+@b+@a<>'000')  THEN 'milhões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=9)  AND (@c+@b+@a<>'000')  THEN 'mil milhões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  AND (@orlen=13)AND(@a='1')   THEN 'bilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  THEN 'biliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=15)  AND (@c+@b+@a<>'000')  THEN 'mil biliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=18)  AND (@c+@b+@a<>'000') AND (@orlen=19)AND(@a='1')   THEN 'trilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=18)   AND (@c+@b+@a<>'000') THEN 'triliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=21)  AND (@c+@b+@a<>'000')  THEN 'mil triliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=24)  AND (@c+@b+@a<>'000') AND (@orlen=25)AND(@a='1')   THEN 'quadrilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=24)  AND (@c+@b+@a<>'000')  THEN 'quadriliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=27)  AND (@c+@b+@a<>'000') THEN 'mil quadriliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=30)  AND (@c+@b+@a<>'000')  AND (@orlen=31)AND(@a='1')   THEN 'quintilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=30)  AND (@c+@b+@a<>'000')   THEN 'quintiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=33)  AND (@c+@b+@a<>'000')   THEN 'mil quintiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=36)  AND (@c+@b+@a<>'000') AND (@orlen=37)AND(@a='1')   THEN 'sextilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=36)  AND (@c+@b+@a<>'000')  THEN 'sextiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=39)  AND (@c+@b+@a<>'000')  THEN 'mil sextiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=42)  AND (@c+@b+@a<>'000')  AND (@orlen=43)AND(@a='1')   THEN 'septilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=42)  AND (@c+@b+@a<>'000')  THEN 'septiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=45)  AND (@c+@b+@a<>'000')  THEN 'mil septiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=48)  AND (@c+@b+@a<>'000') AND (@orlen=49)AND(@a='1')   THEN 'octiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=48)   AND (@c+@b+@a<>'000')  THEN 'octiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=51)  AND (@c+@b+@a<>'000')   THEN 'mil octiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=54)  AND (@c+@b+@a<>'000')  AND (@orlen=55)AND(@a='1')   THEN 'nonilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=54)  AND (@c+@b+@a<>'000')  THEN 'noniliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=57) THEN 'mil noniliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'um'
WHEN '2' THEN 'dois'
WHEN '3' THEN 'três'
WHEN '4' THEN 'quatro'
WHEN '5' THEN 'cinco'
WHEN '6' THEN 'seis'
WHEN '7' THEN 'sete'
WHEN '8' THEN 'oito'
WHEN '9' THEN 'nove'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1'
SET @result=case @a 
WHEN '0' THEN 'dez'
WHEN '1' THEN 'onze'
WHEN '2' THEN 'doze'
WHEN '3' THEN 'treze'
WHEN '4' THEN 'catorze'
WHEN '5' THEN 'quinze'
WHEN '6' THEN 'dezasseis'
WHEN '7' THEN 'dezassete'
WHEN '8' THEN 'dezoito'
WHEN '9' THEN 'dezanove'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 
WHEN '2' THEN 'vinte'
WHEN '3' THEN 'trinta'
WHEN '4' THEN 'quarenta'
WHEN '5' THEN 'cinquenta'
WHEN '6' THEN 'sessenta'

WHEN '7' THEN 'setenta'
WHEN '8' THEN 'oitenta'
WHEN '9' THEN 'noventa'
ELSE ''
END+case @a WHEN '0' THEN '' ELSE ' e' END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
IF @a='0'  AND @b='0' 
SET @result=case @c 
WHEN '1' THEN 'e cem'
WHEN '2' THEN 'e duzentos'
WHEN '3' THEN 'e trezentos'
WHEN '4' THEN 'e quatrocentos'
WHEN '5' THEN 'e quinhentos'
WHEN '6' THEN 'e seiscentos'
WHEN '7' THEN 'e setecentos'
WHEN '8' THEN 'e oitocentos'
WHEN '9' THEN 'e novecentos'
ELSE ''
END+' '+@result
ELSE
SET @result=case @c 
WHEN '1' THEN 'cento e'
WHEN '2' THEN 'duzentos e'
WHEN '3' THEN 'trezentos e'
WHEN '4' THEN 'quatrocentos e'
WHEN '5' THEN 'quinhentos e'
WHEN '6' THEN 'seiscentos e'
WHEN '7' THEN 'setecentos e'
WHEN '8' THEN 'oitocentos e'
WHEN '9' THEN 'novecentos e'
ELSE ''
END+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
IF left(@result,7)='um mil '
SET @result=right(@result,LEN(@result)-3)
IF left(@result,2)='e '
SET @result=right(@result,LEN(@result)-2)
While CHARINDEX('  ', @result)>0
SET @result=REPLACE(@result,'  ',' ')
IF dbo.INCLUDED('milhões', @result)>1
SET @result=REPLACE(@result,'mil milhões','mil')
IF dbo.INCLUDED('biliões', @result)>1
SET @result=REPLACE(@result,'mil biliões','mil')
IF dbo.INCLUDED('triliões', @result)>1
SET @result=REPLACE(@result,'mil triliões','mil')
IF dbo.INCLUDED('quadriliões', @result)>1
SET @result=REPLACE(@result,'mil quadriliões','mil')
IF dbo.INCLUDED('quintiliões', @result)>1
SET @result=REPLACE(@result,'mil quintiliões','mil')
IF dbo.INCLUDED('sextiliões', @result)>1
SET @result=REPLACE(@result,'mil sextiliões','mil')
IF dbo.INCLUDED('septiliões', @result)>1
SET @result=REPLACE(@result,'mil septiliões','mil')
IF dbo.INCLUDED('octiliões', @result)>1
SET @result=REPLACE(@result,'mil octiliões','mil')
IF dbo.INCLUDED('noniliões', @result)>1
SET @result=REPLACE(@result,'mil noniliões','mil')
return @result
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE sayit (@s VARCHAR(255))
 AS
declare @o int, @ret int
exec sp_oacreate 'speech.voicetext', @o out
exec sp_oamethod @o, 'register', NULL, 'say', 'this'
exec sp_OAGetProperty @o,'Enabled', @ret OUTPUT
if @ret=0 
BEGIN
print'voice text capability is disabled'
return
END
exec sp_oasetproperty @o, 'speed', 150
exec sp_oamethod @o, 'speak', NULL, @s, 528
exec sp_OAGetProperty @o,'isspeaking', @ret OUTPUT
WHILE @ret=1
BEGIN
waitfor delay '00:00:01'
exec sp_OAGetProperty @o,'isspeaking', @ret OUTPUT
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE sp_INCLUDED (@s varchar(255),@p varchar(255), @counter int out)
 --Returns how many times a string is included (occurs) into another one.
AS
DECLARE @i int,@c int
SET @i=1
SET @c=0
WHILE charindex(@s, @p, @i)>0
BEGIN
SET @i=charindex(@s, @p, @i)+1
SET @c=@c+1
END
SET @counter=  @c
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE sp_NumbersWords(@s VARCHAR(50), @words VARCHAR(8000) out) 
--English words for numbers
as
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='zero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
print @c+@b+@a
SET @result=case 
WHEN (@i-1=3) AND (@c+@b+@a<>'000') THEN 'thousand'
WHEN (@i-1=6) AND (@c+@b+@a<>'000')  THEN 'million'
WHEN (@i-1=9) AND (@c+@b+@a<>'000')  THEN 'billion'
WHEN (@i-1=12) AND (@c+@b+@a<>'000')  THEN 'trillion'
WHEN (@i-1=15) AND (@c+@b+@a<>'000')  THEN 'quadrillion'
WHEN (@i-1=18) AND (@c+@b+@a<>'000')  THEN 'quintillion'
WHEN (@i-1=21) AND (@c+@b+@a<>'000')  THEN 'sextillion'
WHEN (@i-1=24) AND (@c+@b+@a<>'000')  THEN 'septillion'
WHEN (@i-1=27) AND (@c+@b+@a<>'000') THEN 'octillion'
WHEN (@i-1=30)  AND (@c+@b+@a<>'000') THEN 'nonillion'
WHEN (@i-1=33) AND (@c+@b+@a<>'000') THEN 'decillion'
WHEN (@i-1=36) AND (@c+@b+@a<>'000')  THEN 'undecillion'
WHEN (@i-1=39) AND (@c+@b+@a<>'000') THEN 'duodecillion'
WHEN (@i-1=42) AND (@c+@b+@a<>'000') THEN 'tredecillion'
WHEN (@i-1=45) AND (@c+@b+@a<>'000') THEN 'quattuordecillion'
WHEN (@i-1=48) AND (@c+@b+@a<>'000') THEN 'quindecillion'
WHEN (@i-1=51) AND (@c+@b+@a<>'000')  THEN 'sexdecillion'
WHEN (@i-1=54) AND (@c+@b+@a<>'000')  THEN 'septendecillion'
WHEN (@i-1=57) AND (@c+@b+@a<>'000')   THEN 'octodecillion'
WHEN (@i-1=60) THEN 'novemdecillion'
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'one'
WHEN '2' THEN 'two'
WHEN '3' THEN 'three'
WHEN '4' THEN 'four'
WHEN '5' THEN 'five'
WHEN '6' THEN 'six'
WHEN '7' THEN 'seven'
WHEN '8' THEN 'eight'
WHEN '9' THEN 'nine'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1'
SET @result=case @a 
WHEN '0' THEN 'ten'
WHEN '1' THEN 'eleven'
WHEN '2' THEN 'twelve'
WHEN '3' THEN 'thirteen'
WHEN '4' THEN 'fourteen'
WHEN '5' THEN 'fifteen'
WHEN '6' THEN 'sixteen'
WHEN '7' THEN 'seventeen'
WHEN '8' THEN 'eighteen'
WHEN '9' THEN 'nineteen'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 
WHEN '2' THEN 'twenty'
WHEN '3' THEN 'thirty'
WHEN '4' THEN 'fourty'
WHEN '5' THEN 'fifty'
WHEN '6' THEN 'sixty'
WHEN '7' THEN 'seventy'
WHEN '8' THEN 'eighty'
WHEN '9' THEN 'ninety'
ELSE ''
END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
SET @result=case @c 
WHEN '1' THEN 'one'
WHEN '2' THEN 'two'
WHEN '3' THEN 'three'
WHEN '4' THEN 'four'
WHEN '5' THEN 'five'
WHEN '6' THEN 'six'
WHEN '7' THEN 'seven'
WHEN '8' THEN 'eight'
WHEN '9' THEN 'nine'
ELSE ''
END+' hundred'+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
set @words= @result
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  sp_NúmerosPalabras(@s VARCHAR(50), @words VARCHAR(8000) out) 
--Spanish words for numbers
AS
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='cero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
SET @result=case 
WHEN (@i-1=3)  AND (@c+@b+@a<>'000') THEN 'mil'
WHEN (@i-1=6)  AND (@c+@b+@a<>'000')  AND(@orlen=7)AND(@a='1')   THEN 'millón '
WHEN (@i-1=6)  AND (@c+@b+@a<>'000') THEN 'millones '
WHEN (@i-1=9)  AND (@c+@b+@a<>'000')  THEN 'mil millones'
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  AND (@orlen=13)AND(@a='1')   THEN 'billón '
WHEN (@i-1=12)  AND (@c+@b+@a<>'000') THEN 'billones '
WHEN (@i-1=15)  AND (@c+@b+@a<>'000') THEN 'mil billones'
WHEN (@i-1=18)  AND (@c+@b+@a<>'000')  AND (@orlen=19)AND(@a='1')   THEN 'trillón '
WHEN (@i-1=18)   AND (@c+@b+@a<>'000') THEN 'trillones '
WHEN (@i-1=21)  AND (@c+@b+@a<>'000')  THEN 'mil trillones'
WHEN (@i-1=24)  AND (@c+@b+@a<>'000')  AND (@orlen=25)AND(@a='1')   THEN 'quadrillón '
WHEN  (@i-1=24)  AND (@c+@b+@a<>'000')  THEN 'quadrillones '
WHEN (@i-1=27)  AND (@c+@b+@a<>'000')   THEN 'mil quadrillones'
WHEN (@i-1=30)  AND (@c+@b+@a<>'000')  AND (@orlen=31)AND(@a='1')   THEN 'quintillón '
WHEN  (@i-1=30)  AND (@c+@b+@a<>'000')  THEN 'quintillones '
WHEN (@i-1=33)  AND (@c+@b+@a<>'000')  THEN 'mil quintillones'
WHEN (@i-1=36)  AND (@c+@b+@a<>'000')  AND (@orlen=37)AND(@a='1')   THEN 'sextillón '
WHEN  (@i-1=36)  AND (@c+@b+@a<>'000')  THEN 'sextillones '
WHEN (@i-1=39)  AND (@c+@b+@a<>'000')  THEN 'mil sextillones'
WHEN (@i-1=42)  AND (@c+@b+@a<>'000')  AND (@orlen=43)AND(@a='1')   THEN 'septillón '
WHEN  (@i-1=42)  AND (@c+@b+@a<>'000')   THEN 'septillones '
WHEN (@i-1=45)  AND (@c+@b+@a<>'000')  THEN 'mil septillones'
WHEN (@i-1=48)  AND (@c+@b+@a<>'000') AND (@orlen=49)AND(@a='1')   THEN 'octillón '
WHEN  (@i-1=48)   AND (@c+@b+@a<>'000')  THEN 'octillones '
WHEN (@i-1=51)  AND (@c+@b+@a<>'000')   THEN 'mil octillones'
WHEN (@i-1=54)  AND (@c+@b+@a<>'000')  AND (@orlen=55)AND(@a='1')   THEN 'nonillón '
WHEN  (@i-1=54)  AND (@c+@b+@a<>'000')  THEN 'nonillones '
WHEN (@i-1=57) THEN 'mil nonillones'
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'un'
WHEN '2' THEN 'dos'
WHEN '3' THEN 'tres'
WHEN '4' THEN 'cuatro'
WHEN '5' THEN 'cinco'
WHEN '6' THEN 'seis'
WHEN '7' THEN 'siete'
WHEN '8' THEN 'ocho'
WHEN '9' THEN 'nueve'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1' OR @b='2' 
SET @result=case @b+@a 
WHEN '10' THEN 'diez'
WHEN '11' THEN 'once'
WHEN '12' THEN 'doce'
WHEN '13' THEN 'trece'
WHEN '14' THEN 'catorce'
WHEN '15' THEN 'quince'
WHEN '16' THEN 'dieciséis'
WHEN '17' THEN 'diecisiete'
WHEN '18' THEN 'dieciocho'
WHEN '19' THEN 'diecinueve'
WHEN '20' THEN 'veinte'
WHEN '21' THEN 'veintiuno'
WHEN '22' THEN 'veintidós'
WHEN '23' THEN 'veintitrés'
WHEN '24' THEN 'veinticuatro'
WHEN '25' THEN 'veinticinco'
WHEN '26' THEN 'veintiséis'
WHEN '27' THEN 'veintisiete'

WHEN '28' THEN 'veintiocho'
WHEN '29' THEN 'veintinueve'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 

WHEN '3' THEN 'treinta'
WHEN '4' THEN 'cuarenta'
WHEN '5' THEN 'cincuenta'
WHEN '6' THEN 'sesenta'
WHEN '7' THEN 'setenta'
WHEN '8' THEN 'ochenta'
WHEN '9' THEN 'noventa'
ELSE ''
END+case @a WHEN '0' THEN '' ELSE ' y' END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
IF @a='0'  AND @b='0' 
SET @result=case @c 
WHEN '1' THEN 'cien'
WHEN '2' THEN 'doscientos'
WHEN '3' THEN 'trescientos'
WHEN '4' THEN 'cuatrocientos'
WHEN '5' THEN 'quinientos'
WHEN '6' THEN 'seiscientos'
WHEN '7' THEN 'setecientos'
WHEN '8' THEN 'ochocientos'
WHEN '9' THEN 'novecientos'
ELSE ''
END+' '+@result
ELSE
SET @result=case @c 
WHEN '1' THEN 'ciento'
WHEN '2' THEN 'doscientos'
WHEN '3' THEN 'trescientos'
WHEN '4' THEN 'cuatrocientos'
WHEN '5' THEN 'quinientos'
WHEN '6' THEN 'seiscientos'
WHEN '7' THEN 'setecientos'
WHEN '8' THEN 'ochocientos'
WHEN '9' THEN 'novecientos'
ELSE ''
END+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
IF left(@result,7)='un mil '
SET @result=right(@result,LEN(@result)-3)
IF right(@result,3)=' un'
SET @result=@result+'o'
IF left(@result,2)='y '
SET @result=right(@result,LEN(@result)-2)
While CHARINDEX('  ', @result)>0
SET @result=REPLACE(@result,'  ',' ')
exec sp_INCLUDED 'millones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil millones','mil')
exec sp_INCLUDED 'billones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil billones','mil')
exec sp_INCLUDED 'trillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil trillones','mil')
exec sp_INCLUDED 'quadrillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil quadrillones','mil')
exec sp_INCLUDED 'quintillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil quintillones','mil')
exec sp_INCLUDED 'sextillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil sextillones','mil')
exec sp_INCLUDED 'septillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil septillones','mil')
exec sp_INCLUDED 'octillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil octillones','mil')
exec sp_INCLUDED 'nonillones', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil nonillones','mil')
set @words= @result
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  sp_NúmerosPalavras(@s VARCHAR(50), @words  VARCHAR(8000) out ) 
--Portuguese words for numbers
as
DECLARE @a char(1),@b char(1),@c char(1),@i int, @j int, @result VARCHAR(1024), @orlen int
SET @orlen=LEN(@s)
IF LEN(@s) % 3>0
SET @s=' '+@S
IF LEN(@s) % 3>0
SET @s=' '+@S
SET @i=1 SET @result=''
IF LEN(@s)=1 AND @s='0' 
SET @result='zero'
WHILE @i<=LEN(@s)
BEGIN
SET @j=LEN(@s)-@i+1
SET @a=substring(@s,@j,1)
SET @b=substring(@s,@j-1,1)
SET @c=substring(@s,@j-2,1)
if isnumeric(@a)=1
BEGIN
SET @result=case 
WHEN (@i-1=3)  AND (@c+@b+@a<>'000') THEN 'mil'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=6)  AND (@c+@b+@a<>'000')  AND(@orlen=7)AND(@a='1')   THEN 'milhão '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=6)  AND (@c+@b+@a<>'000') THEN 'milhões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=9)  AND (@c+@b+@a<>'000')  THEN 'mil milhões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  AND (@orlen=13)AND(@a='1')   THEN 'bilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=12)  AND (@c+@b+@a<>'000')  THEN 'biliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=15)  AND (@c+@b+@a<>'000')  THEN 'mil biliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=18)  AND (@c+@b+@a<>'000') AND (@orlen=19)AND(@a='1')   THEN 'trilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=18)   AND (@c+@b+@a<>'000') THEN 'triliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=21)  AND (@c+@b+@a<>'000')  THEN 'mil triliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=24)  AND (@c+@b+@a<>'000')  AND (@orlen=25)AND(@a='1')   THEN 'quadrilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=24)  AND (@c+@b+@a<>'000') THEN 'quadriliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=27)  AND (@c+@b+@a<>'000')  THEN 'mil quadriliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=30)  AND (@c+@b+@a<>'000') AND (@orlen=31)AND(@a='1')   THEN 'quintilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=30)  AND (@c+@b+@a<>'000') THEN 'quintiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=33)  AND (@c+@b+@a<>'000')  THEN 'mil quintiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=36)  AND (@c+@b+@a<>'000')  AND (@orlen=37)AND(@a='1')   THEN 'sextilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=36)  AND (@c+@b+@a<>'000')  THEN 'sextiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=39)  AND (@c+@b+@a<>'000') THEN 'mil sextiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=42)  AND (@c+@b+@a<>'000') AND (@orlen=43)AND(@a='1')   THEN 'septilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=42)  AND (@c+@b+@a<>'000')  THEN 'septiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=45)  AND (@c+@b+@a<>'000') THEN 'mil septiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=48)  AND (@c+@b+@a<>'000')  AND (@orlen=49)AND(@a='1')   THEN 'octiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=48)   AND (@c+@b+@a<>'000') THEN 'octiliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=51)  AND (@c+@b+@a<>'000')  THEN 'mil octiliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=54)  AND (@c+@b+@a<>'000')  AND (@orlen=55)AND(@a='1')   THEN 'nonilião '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN  (@i-1=54)  AND (@c+@b+@a<>'000')   THEN 'noniliões '
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
WHEN (@i-1=57) THEN 'mil noniliões'
+CASE WHEN (substring(@s,@i,1)='0') AND (substring(@s,LEN(@s)-1,1)!='0') THEN ' e ' WHEN  (substring(@s,@i,1)='0') AND (right(@S,1)!='0') THEN ' e ' ELSE '' END
ELSE ''
END+' '+@result
if @b!='1' OR @b=' '
SET @result=case @a 
WHEN '1' THEN 'um'
WHEN '2' THEN 'dois'
WHEN '3' THEN 'três'
WHEN '4' THEN 'quatro'
WHEN '5' THEN 'cinco'
WHEN '6' THEN 'seis'
WHEN '7' THEN 'sete'
WHEN '8' THEN 'oito'
WHEN '9' THEN 'nove'
ELSE ''
END+' '+@result
if (isnumeric(@b)=1 )AND (@b!='0')
IF @b='1'
SET @result=case @a 
WHEN '0' THEN 'dez'
WHEN '1' THEN 'onze'
WHEN '2' THEN 'doze'
WHEN '3' THEN 'treze'
WHEN '4' THEN 'catorze'
WHEN '5' THEN 'quinze'
WHEN '6' THEN 'dezasseis'
WHEN '7' THEN 'dezassete'
WHEN '8' THEN 'dezoito'
WHEN '9' THEN 'dezanove'
ELSE ''
END+' '+@result
ELSE
SET @result=case @b 
WHEN '2' THEN 'vinte'
WHEN '3' THEN 'trinta'
WHEN '4' THEN 'quarenta'
WHEN '5' THEN 'cinquenta'
WHEN '6' THEN 'sessenta'
WHEN '7' THEN 'setenta'
WHEN '8' THEN 'oitenta'
WHEN '9' THEN 'noventa'
ELSE ''
END+case @a WHEN '0' THEN '' ELSE ' e' END+' '+@result
END
if (isnumeric(@c)=1)AND (@c!='0')
IF @a='0'  AND @b='0' 
SET @result=case @c 
WHEN '1' THEN 'e cem'
WHEN '2' THEN 'e duzentos'
WHEN '3' THEN 'e trezentos'
WHEN '4' THEN 'e quatrocentos'
WHEN '5' THEN 'e quinhentos'
WHEN '6' THEN 'e seiscentos'
WHEN '7' THEN 'e setecentos'
WHEN '8' THEN 'e oitocentos'
WHEN '9' THEN 'e novecentos'
ELSE ''
END+' '+@result
ELSE
SET @result=case @c 
WHEN '1' THEN 'cento e'
WHEN '2' THEN 'duzentos e'
WHEN '3' THEN 'trezentos e'
WHEN '4' THEN 'quatrocentos e'
WHEN '5' THEN 'quinhentos e'
WHEN '6' THEN 'seiscentos e'
WHEN '7' THEN 'setecentos e'
WHEN '8' THEN 'oitocentos e'
WHEN '9' THEN 'novecentos e'
ELSE ''
END+' '+@result
SET @i=@i+3
END
SET @result=LTRIM(RTRIM(@result))
IF left(@result,7)='um mil '
SET @result=right(@result,LEN(@result)-3)
IF left(@result,2)='e '
SET @result=right(@result,LEN(@result)-2)
While CHARINDEX('  ', @result)>0
SET @result=REPLACE(@result,'  ',' ')
exec sp_INCLUDED 'milhões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil milhões','mil')
exec sp_INCLUDED 'biliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil biliões','mil')
exec sp_INCLUDED 'triliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil triliões','mil')
exec sp_INCLUDED 'quadriliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil quadriliões','mil')
exec sp_INCLUDED 'quintiliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil quintiliões','mil')
exec sp_INCLUDED 'sextiliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil sextiliões','mil')
exec sp_INCLUDED 'septiliões', @result, @i out
IF @i>1
SET @result=REPLACE(@result,'mil septiliões','mil')
exec sp_INCLUDED 'octiliões', @result, @i out
IF @i>1
IF dbo.INCLUDED('octiliões', @result)>1
SET @result=REPLACE(@result,'mil octiliões','mil')
exec sp_INCLUDED 'noniliões', @result, @i out
IF @i>1
IF dbo.INCLUDED('noniliões', @result)>1
SET @result=REPLACE(@result,'mil noniliões','mil')
set @words= @result
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating