How to find the count of letter "L" in HELLO

Answer Posted / ajit nayak

FUNCTION
---------
CREATE OR REPLACE FUNCTION FN_WORD_CNT (V_NAME VARCHAR2,V_SRCH_WRD VARCHAR2)
RETURN NUMBER
IS

I_COUNT NUMBER;
I_WRD_CNT NUMBER;

TYPE V_WORD IS TABLE OF VARCHAR2(10)
INDEX BY BINARY_INTEGER;
V_S_WORD V_WORD;

BEGIN
I_WRD_CNT := LENGTH(V_NAME);
DBMS_OUTPUT.PUT_LINE(I_WRD_CNT);
I_COUNT := 0;

FOR I IN 1..I_WRD_CNT
LOOP
SELECT SUBSTR(V_NAME,I,1)
INTO V_S_WORD(I)
FROM DUAL;

END LOOP;
FOR I IN 1..V_S_WORD.COUNT
LOOP

IF(V_S_WORD(I) = V_SRCH_WRD) THEN
I_COUNT := I_COUNT + 1;

END IF;
END LOOP;
RETURN I_COUNT;
END;

FOR RUN
-------

select FN_WORD_CNT('AJIT','A')
from dual;

PROCEDURE
---------
CREATE OR REPLACE FUNCTION FN_WORD_CNT (V_NAME VARCHAR2,V_SRCH_WRD VARCHAR2)
RETURN NUMBER
IS

I_COUNT NUMBER;
I_WRD_CNT NUMBER;

TYPE V_WORD IS TABLE OF VARCHAR2(10)
INDEX BY BINARY_INTEGER;
V_S_WORD V_WORD;

BEGIN
I_WRD_CNT := LENGTH(V_NAME);
DBMS_OUTPUT.PUT_LINE(I_WRD_CNT);
I_COUNT := 0;

FOR I IN 1..I_WRD_CNT
LOOP
SELECT SUBSTR(V_NAME,I,1)
INTO V_S_WORD(I)
FROM DUAL;

END LOOP;
FOR I IN 1..V_S_WORD.COUNT
LOOP

IF(V_S_WORD(I) = V_SRCH_WRD) THEN
I_COUNT := I_COUNT + 1;

END IF;
END LOOP;
RETURN I_COUNT;
END;

RUN THIS PROCEDURE
------------------
DECLARE
v_name varchar2(10) := '&name';
v_srch varchar(2) := '&str';
i_cnt number;
begin
DBMS_OUTPUT.PUT_LINE('ENTER NAME '||v_name||CHR(10)||' SEARCH STR '||v_srch);
P_FN_WORD_CNT(v_name,v_srch,i_cnt);
END;

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the two parts of design view?

683


Explain the purpose of %type and %rowtype data types?

591


Why do we use triggers?

620


What do you mean by rowid?

617


How many clustered indexes can you have?

637






What are pl/sql cursor exceptions?

669


What are the possible values for the boolean data field?

596


Could you please provide oca (oracle 10g) dumps for my certification ?

4643


How do you create a db file?

603


Which join is like inner join?

669


Which version of sql do I have?

662


Why query optimization is needed?

610


What is the difference between sql and mysql?

653


Define union, minus, union all, intersect ?

652


What is prepared statement in sql?

660