Count no. of characters and words in a string in PL/SQL Last Updated : 05 Jul, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to count the number of characters and words in the given string. Examples: Input: str = 'Geeks for geeks ' Output: Characters = 13 , Words = 3 Input: str = 'A Computer science portal' Output: Characters = 22, Words = 4 Approach is to maintain two counter variables i.e. one for the characters and the other for words. Start traversing character one by one and increment the count and when there is a blank space then increment the count of words. Below is the required implementation: SQL DECLARE -- Declare required variables str VARCHAR2(40) := 'Geeks for Geeks'; noofchars NUMBER(4) := 0; noofwords NUMBER(4) := 1; s CHAR; BEGIN FOR i IN 1..Length(str) LOOP s := Substr(str, i, 1); -- Count no. of characters noofchars := noofchars + 1; -- Count no. of words IF s = ' ' THEN noofwords := noofwords + 1; END IF; END LOOP; dbms_output.Put_line('No. of characters:' ||noofchars); dbms_output.Put_line('No. of words: ' ||noofwords); END; -- Program End Output : No. of characters:15 No. of words: 3 Comment More infoAdvertise with us Next Article Count no. of characters and words in a string in PL/SQL S Shashank12 Follow Improve Article Tags : Misc SQL SQL-PL/SQL Practice Tags : Misc Similar Reads No. of vowels and consonants in a given string in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to find the number of vo 2 min read Count Occurrences of a Given Character in a String Given a string S and a character 'c', the task is to count the occurrence of the given character in the string.Examples: Input : S = "geeksforgeeks" and c = 'e'Output : 4Explanation: 'e' appears four times in str.Input : S = "abccdefgaa" and c = 'a' Output : 3Explanation: 'a' appears three times in 6 min read How to Get First Character of a String in SQL? SQL (Structured Query Language) is essential for managing and querying relational databases. Whether you're handling customer data, employee records, or product details, SQL provides powerful tools for manipulating string data. One common task when working with strings is to extract the first charac 4 min read Concatenation of strings in PL/SQL Prerequisite - PL/SQL Introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given two strings and the task is to concatenate them a 1 min read Check whether a string is palindrome or not in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to find whether it is Pa 1 min read Like