SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Sum of the first and last digit of a number in PL/SQL

To compute the sum of the first and last digits of a number in PL/SQL, you can extract these digits from the number using arithmetic operations and then sum them up. Here's a simple PL/SQL block to do this:

DECLARE
    n NUMBER := 12345; -- example number
    last_digit NUMBER;
    first_digit NUMBER;
BEGIN
    -- Get the last digit
    last_digit := MOD(n, 10);

    -- Loop to get the first digit
    WHILE n >= 10 LOOP
        n := TRUNC(n / 10);
    END LOOP;

    first_digit := n;

    -- Display the sum of first and last digit
    DBMS_OUTPUT.PUT_LINE('Sum of the first and last digit: ' || (first_digit + last_digit));
END;
/

In the example provided, for the number 12345, the last digit is 5 and the first digit is 1, so the result displayed will be 6. You can replace 12345 with any other number to compute the sum of its first and last digits.

  1. PL/SQL Program to Find the Sum of First and Last Digit of a Number:

    DECLARE
        num INTEGER := 12345;
        first_digit INTEGER;
        last_digit INTEGER;
    BEGIN
        first_digit := TO_NUMBER(SUBSTR(num, 1, 1));
        last_digit := TO_NUMBER(SUBSTR(num, -1, 1));
    
        DBMS_OUTPUT.PUT_LINE('Sum of first and last digits: ' || (first_digit + last_digit));
    END;
    /
    
  2. Oracle SQL Function for Calculating Sum of First and Last Digit:

    CREATE OR REPLACE FUNCTION calculate_first_last_digit_sum (
        num IN INTEGER
    ) RETURN INTEGER IS
        first_digit INTEGER;
        last_digit INTEGER;
    BEGIN
        first_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), 1, 1));
        last_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), -1, 1));
    
        RETURN first_digit + last_digit;
    END calculate_first_last_digit_sum;
    /
    
  3. PL/SQL Procedure for First and Last Digit Summation:

    CREATE OR REPLACE PROCEDURE calculate_first_last_digit_sum_proc (
        num IN INTEGER
    ) IS
        first_digit INTEGER;
        last_digit INTEGER;
    BEGIN
        first_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), 1, 1));
        last_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), -1, 1));
    
        DBMS_OUTPUT.PUT_LINE('Sum of first and last digits: ' || (first_digit + last_digit));
    END calculate_first_last_digit_sum_proc;
    /
    
  4. Oracle SQL Query for Sum of First and Last Digit of a Number:

    SELECT
        num,
        TO_NUMBER(SUBSTR(TO_CHAR(num), 1, 1)) + TO_NUMBER(SUBSTR(TO_CHAR(num), -1, 1)) AS first_last_digit_sum
    FROM
        your_table;
    
  5. Calculating Total of First and Last Digit in PL/SQL:

    DECLARE
        num INTEGER := 56789;
        first_digit INTEGER;
        last_digit INTEGER;
    BEGIN
        first_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), 1, 1));
        last_digit := TO_NUMBER(SUBSTR(TO_CHAR(num), -1, 1));
    
        DBMS_OUTPUT.PUT_LINE('Total of first and last digits: ' || (first_digit + last_digit));
    END;
    /
    
  6. Oracle SQL Script to Find Sum of First and Last Digit:

    SELECT
        num,
        TO_NUMBER(SUBSTR(TO_CHAR(num), 1, 1)) + TO_NUMBER(SUBSTR(TO_CHAR(num), -1, 1)) AS first_last_digit_sum
    FROM
        your_table;