I have this function that converts a number (amount) to words, I understand the result of this function, but I don't know how it works, as I don't know how to convert the loop into SQL
FUNCTION number_to_word(p_number IN NUMBER) RETURN VARCHAR2 AS
TYPE myArray IS TABLE OF VARCHAR2(255);
l_str myArray := myArray('',
' thousand ',
' million ',
' billion ',
' trillion ',
' quadrillion ',
' quintillion ',
' sextillion ',
' septillion ',
' octillion ',
' nonillion ',
' decillion ',
' undecillion ',
' duodecillion ');
l_num VARCHAR2(50) DEFAULT trunc(p_number);
l_return VARCHAR2(32767);
BEGIN
IF (l_num IS NULL OR l_num = 0) THEN
l_return := 'Zero';
ELSE
FOR i IN 1 .. l_str.count LOOP
EXIT WHEN l_num IS NULL;
IF (to_number(substr(l_num, length(l_num) - 2, 3)) <> 0) THEN
l_return := to_char(to_date(substr(l_num, length(l_num) - 2, 3),
'J'),
'Jsp') || l_str(i) || l_return;
END IF;
l_num := substr(l_num, 1, length(l_num) - 3);
END LOOP;
END IF;
RETURN l_return;
END number_to_word;