SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Check whether a string is palindrome or not in PL/SQL

To check if a string is a palindrome in PL/SQL, you can create a simple function. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

Here's how you can create a PL/SQL function to check for palindromes:

CREATE OR REPLACE FUNCTION IsPalindrome(p_str VARCHAR2) RETURN VARCHAR2 IS
    v_cleaned_str VARCHAR2(1000);
    v_reversed_str VARCHAR2(1000);
BEGIN
    -- Remove non-alphanumeric characters and convert to uppercase
    FOR i IN 1..LENGTH(p_str) LOOP
        IF SUBSTR(p_str, i, 1) BETWEEN 'A' AND 'Z' OR SUBSTR(p_str, i, 1) BETWEEN '0' AND '9' THEN
            v_cleaned_str := v_cleaned_str || SUBSTR(p_str, i, 1);
        ELSIF SUBSTR(p_str, i, 1) BETWEEN 'a' AND 'z' THEN
            v_cleaned_str := v_cleaned_str || UPPER(SUBSTR(p_str, i, 1));
        END IF;
    END LOOP;

    -- Reverse the cleaned string
    FOR i IN REVERSE 1..LENGTH(v_cleaned_str) LOOP
        v_reversed_str := v_reversed_str || SUBSTR(v_cleaned_str, i, 1);
    END LOOP;

    -- Check if cleaned string is same as its reverse
    IF v_cleaned_str = v_reversed_str THEN
        RETURN 'YES';  -- It's a palindrome
    ELSE
        RETURN 'NO';   -- It's not a palindrome
    END IF;
END IsPalindrome;
/

To use the function, you can create a PL/SQL block:

DECLARE
    v_str      VARCHAR2(100) := 'A man, a plan, a canal, Panama';  -- Example string
    v_result   VARCHAR2(3);
BEGIN
    v_result := IsPalindrome(v_str);
    DBMS_OUTPUT.PUT_LINE('Is the string a palindrome? ' || v_result);
END;
/

This function not only checks if a word is a palindrome but also works with phrases, ignoring punctuation, spaces, and case differences. Adjust the v_str variable as needed to test other strings.

  1. PL/SQL program to check if a string is a palindrome:

    CREATE OR REPLACE FUNCTION is_palindrome(p_str IN VARCHAR2) RETURN BOOLEAN IS
       v_reversed VARCHAR2(4000);
    BEGIN
       v_reversed := REVERSE(p_str);
       RETURN p_str = v_reversed;
    END;
    /
    
  2. Oracle SQL function for palindrome detection in strings:

    CREATE OR REPLACE FUNCTION is_palindrome_sql(p_str VARCHAR2) RETURN BOOLEAN IS
    BEGIN
       RETURN p_str = REVERSE(p_str);
    END;
    /
    
  3. PL/SQL procedure for palindrome validation:

    If you prefer a procedure, you can modify the first example like this:

    CREATE OR REPLACE PROCEDURE check_palindrome(p_str IN VARCHAR2) IS
       v_reversed VARCHAR2(4000);
    BEGIN
       v_reversed := REVERSE(p_str);
       IF p_str = v_reversed THEN
          DBMS_OUTPUT.PUT_LINE(p_str || ' is a palindrome.');
       ELSE
          DBMS_OUTPUT.PUT_LINE(p_str || ' is not a palindrome.');
       END IF;
    END;
    /
    
  4. Palindrome check in Oracle SQL:

    Using a function in Oracle SQL:

    SELECT is_palindrome_sql('level') AS is_palindrome
    FROM dual;
    
  5. Oracle SQL query for verifying palindrome strings:

    SELECT CASE WHEN is_palindrome_sql('radar') THEN 1 ELSE 0 END AS is_palindrome
    FROM dual;
    
  6. Oracle SQL script to check if a word is palindrome:

    DECLARE
       v_input VARCHAR2(50) := 'civic';
       v_result BOOLEAN;
    BEGIN
       v_result := is_palindrome_sql(v_input);
       IF v_result THEN
          DBMS_OUTPUT.PUT_LINE(v_input || ' is a palindrome.');
       ELSE
          DBMS_OUTPUT.PUT_LINE(v_input || ' is not a palindrome.');
       END IF;
    END;
    /