Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Count the number of words in a phrase using Teradata SQL - response (2) by M.Saeed Khurram

$
0
0

Hi Sree,
I have created a stored procedure to count the number of words in a string, hope it will help you!

REPLACE PROCEDURE SAMPLES.WordCount
 (
 IN InputString VARCHAR(4000) ,
  OUT WCount INT
  )
  
BEGIN

DECLARE Indexs          INT;
DECLARE Charss           CHAR(1);
DECLARE PrevChar       CHAR(1);
DECLARE WordCntt      INT;
 
SET Indexs = 1;
SET WordCntt = 0;

WHILE Indexs <= CHARACTER_LENGTH(InputString)
DO
BEGIN
    SET Charss  = SUBSTR(InputString, indexs, 1);
    SET PrevChar = CASE WHEN INDEXs = 1 THEN ''
                         ELSE SUBSTR(InputString, INDEXs - 1, 1)
                    END;

    IF PrevChar = ''  AND Charss <> ''
    THEN   SET WordCntt = WordCntt + 1;
    END IF;
    
    SET Indexs = Indexs + 1;
END;
END WHILE
;

SET  WCount = WordCntt;

END;

 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>