SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Count no. of characters and words in a string in PL/SQL

To count the number of characters and words in a string in PL/SQL, you can create a simple block. Here's an example that calculates both:

DECLARE
    -- Test string
    input_str VARCHAR2(1000) := 'This is an example string for counting.';
    char_count NUMBER;
    word_count NUMBER;
BEGIN
    -- Counting characters
    char_count := LENGTH(input_str);

    -- Counting words by counting the number of spaces and adding 1.
    -- Assuming words are separated by single spaces and there's no leading or trailing space.
    word_count := LENGTH(input_str) - LENGTH(REPLACE(input_str, ' ', '')) + 1;

    -- Output
    DBMS_OUTPUT.PUT_LINE('Number of characters: ' || char_count);
    DBMS_OUTPUT.PUT_LINE('Number of words: ' || word_count);
END;
/

Note: This example assumes that words in the string are separated by single spaces. It also assumes there are no leading or trailing spaces in the input string. If these assumptions do not hold, you might need a more complex solution, perhaps using regular expressions or other string manipulation functions.

  1. PL/SQL program to count characters and words in a string:

    CREATE OR REPLACE PROCEDURE count_chars_and_words(p_text IN VARCHAR2) IS
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(p_text);
       v_word_count := REGEXP_COUNT(p_text, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END count_chars_and_words;
    /
    
  2. Oracle SQL function for counting characters and words:

    CREATE OR REPLACE FUNCTION count_chars_and_words_sql(p_text VARCHAR2) RETURN VARCHAR2 IS
       v_char_count NUMBER := LENGTH(p_text);
       v_word_count NUMBER := REGEXP_COUNT(p_text, '\b\w+\b');
    BEGIN
       RETURN 'Character count: ' || v_char_count || ', Word count: ' || v_word_count;
    END count_chars_and_words_sql;
    /
    
  3. Calculate number of characters and words in a string using PL/SQL:

    DECLARE
       v_text VARCHAR2(100) := 'Hello World! This is a sample text.';
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(v_text);
       v_word_count := REGEXP_COUNT(v_text, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END;
    /
    
  4. PL/SQL procedure for string analysis - characters and words:

    CREATE OR REPLACE PROCEDURE analyze_string(p_text IN VARCHAR2) IS
       v_char_count NUMBER := LENGTH(p_text);
       v_word_count NUMBER := REGEXP_COUNT(p_text, '\b\w+\b');
    BEGIN
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END analyze_string;
    /
    
  5. Counting letters and words in a string in PL/SQL:

    DECLARE
       v_input_text VARCHAR2(200) := 'PL/SQL is powerful and versatile.';
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(v_input_text);
       v_word_count := REGEXP_COUNT(v_input_text, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END;
    /
    
  6. Oracle SQL query to find character and word count in a string:

    SELECT LENGTH(your_column) AS char_count,
           REGEXP_COUNT(your_column, '\b\w+\b') AS word_count
    FROM your_table;
    
  7. PL/SQL function to determine characters and words in a text:

    CREATE OR REPLACE FUNCTION analyze_text(p_text VARCHAR2) RETURN VARCHAR2 IS
       v_char_count NUMBER := LENGTH(p_text);
       v_word_count NUMBER := REGEXP_COUNT(p_text, '\b\w+\b');
    BEGIN
       RETURN 'Character count: ' || v_char_count || ', Word count: ' || v_word_count;
    END analyze_text;
    /
    
  8. String manipulation in PL/SQL for character and word analysis:

    DECLARE
       v_text VARCHAR2(150) := 'Programming is fun!';
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(v_text);
       v_word_count := REGEXP_COUNT(v_text, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END;
    /
    
  9. Oracle SQL script to count characters and words in a sentence:

    DECLARE
       v_sentence VARCHAR2(200) := 'The quick brown fox jumps over the lazy dog.';
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(v_sentence);
       v_word_count := REGEXP_COUNT(v_sentence, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END;
    /
    
  10. Finding total character and word count in PL/SQL:

    DECLARE
       v_text VARCHAR2(250) := 'Hello World.';
       v_char_count NUMBER;
       v_word_count NUMBER;
    BEGIN
       v_char_count := LENGTH(v_text);
       v_word_count := REGEXP_COUNT(v_text, '\b\w+\b');
       DBMS_OUTPUT.PUT_LINE('Character count: ' || v_char_count);
       DBMS_OUTPUT.PUT_LINE('Word count: ' || v_word_count);
    END;
    /