Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:26:26+00:00 2026-05-24T10:26:26+00:00

I have a table containing house numbers as varchar2 like 10, 10a etc. I

  • 0

I have a table containing house numbers as varchar2 like 10, 10a etc. I want to detect all the rows that are not numeric like ’10a’.
My solution would be to output every row that causes exception when there is an attempt to convert data like ’10a’ to number.

declare
number_correct number;
number_incorrect varchar2(4000);
begin 
for rec in(select '10' house_nr from dual union select '10a' house_nr from dual)
loop
number_correct:=to_number(rec.house_nr);
number_incorrect:=rec.house_nr;
end loop;
exception
when others then
dbms_output.put_line('correct: '||number_correct);
dbms_output.put_line('incorrect: '||number_incorrect);
end;

It should show that incorrect is 10a, but it doesn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:26:27+00:00Added an answer on May 24, 2026 at 10:26 am

    While @Dave is right about the actual question you asked, I think what you’re trying to achieve is different than what you’re doing. As written the PL/SQL block will only evaluate values until it gets to the first non-number. If you want all values evaluated, then you’ll need something like this:

    BEGIN
       FOR rec IN (SELECT '10' house_nr FROM DUAL
                   UNION ALL
                   SELECT '10a' house_nr FROM DUAL
                   UNION ALL
                   SELECT '11' house_nr FROM DUAL) LOOP
          error_fl         := FALSE;
          BEGIN
             number_correct   := TO_NUMBER(rec.house_nr);
          EXCEPTION
             WHEN VALUE_ERROR THEN
                error_fl   := TRUE;
             WHEN OTHERS THEN
                RAISE;
          END;
    
          IF error_fl THEN
             DBMS_OUTPUT.put_line('incorrect: ' || rec.house_nr);
          ELSE
             DBMS_OUTPUT.put_line('correct: ' || rec.house_nr);
          END IF;
       END LOOP;
    END;
    

    By moving the exception handling to inside the loop, we can continue processing after an error is raised. Also, this version only returns the “incorrect” message if the error that occurs is in fact a conversion error, as opposed to an unexpected error. When you’re writing error handling for specific conditions like this, it’s important to be as precise as possible.


    As @Stephen points out, regex is generally faster than PL/SQL procedures or functions and a single SQL statement is typically better than a procedural loop. However, regex functions can only be used in where clauses, so if you want to see results for all values, you’d need to query your table twice:

    WITH test_num as (SELECT '10' house_nr FROM DUAL
                      UNION ALL
                      SELECT '10a' house_nr FROM DUAL
                      UNION ALL
                      SELECT '11' house_nr FROM DUAL)
    SELECT 'correct' AS status, a.*
    FROM   test_num a
    WHERE  REGEXP_LIKE(t1, '[^[:digit:]]')
    UNION ALL
    SELECT 'incorrect', a.*
    FROM   test_num a
    WHERE  NOT REGEXP_LIKE(t1, '[^[:digit:]]');
    

    If you’re not comfortable with regex, or can’t come up with a suitable expression, you can still do this in a single SQL statement by creating your own function:

    CREATE OR REPLACE FUNCTION is_num(p_string VARCHAR2)
       RETURN NUMBER
       DETERMINISTIC IS
       v_test     NUMBER;
    BEGIN
       v_test   := p_string;
       RETURN 1;
    EXCEPTION
       WHEN VALUE_ERROR THEN
          RETURN 0;
       WHEN OTHERS THEN
          RAISE;
    END;
    
    WITH test_num as (SELECT '10' house_nr FROM DUAL
                      UNION ALL
                      SELECT '10a' house_nr FROM DUAL
                      UNION ALL
                      SELECT '11' house_nr FROM DUAL)
    SELECT CASE is_num(t1) WHEN 0 THEN 'correct' ELSE 'incorrect' END AS status, a.*
    FROM   test_num a;
    

    This won’t be quite as fast as using regex in most circumstances, but it does have one advantage: function-based indexes are notoriously finicky about regexp functions, but they shouldn’t have any problem with a function like this. It doesn’t seem like you’ll need an index for this particular query, but it’s something to keep in mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table containing some information that I need. All these rows also
I have one table containing some fields like Firstname,companyname etc.. Now I want the
I have a table containing data which I do not want to wrap onto
I have a table containing reports and the date/time they were created. I'd like
I have a table containing cells with phone numbers. How can I allow the
I have a table containing attributes Id, Emp_name, dept_name, salary . Now i want
I have a table containing multiple addresses, including their Lat/Long coordinates, and I want
I have a table containing numbers, range between 1-1000 they are unique, but I
I have a table containing a large number of rows. Each row has 2
I have a table containing access logs. I want to know how many accesses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.