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 8968067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:23:13+00:00 2026-06-15T17:23:13+00:00

I am using Oracle 11g. I am using the Scott account and the demo

  • 0

I am using Oracle 11g. I am using the Scott account and the demo EMP table. I inserted one record with ENAME BRUCE WILLIAM. My aim is to show the first name and last name in two columns. I used this code:

select trim rpad(ename, instr(ename,' ')))   "First Name", 
       trim(substr(ename, instr(ename,' '))) "Last Name" 
  from emp;

This gives a weird result. The First Name is extended to second line. I used

select trim(substr(ename, 1, instr(ename,' '))), 
       trim(substr(ename, instr(ename, ' '))) 
  from emp;

I got the expected output. My question is why the first line of query is giving extra spaces?

  • 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-06-15T17:23:14+00:00Added an answer on June 15, 2026 at 5:23 pm

    You are not getting extra spaces in your string, and if you were then the trim() would remove them again. SQL*Plus is just formatting the results in a way you don’t expect. The documentation mentions the default formatting for column types, and can usually work it out for system functions (though the characterset can make it bigger than you expect).

    It seems like SQL*Plus, and SQL Developer, can’t determine a sensible default for your rpad case, but can for your substr. Well, SQL*Plus is really just getting a result set cursor from the database, and using the cursor metadata to determine the default widths to apply to the fields for display, so it isn’t getting the length you expect from that metadata. But what length should it use?

    The database only knows how big the rpad value can be if the padding length is a simple value – it doesn’t even mind zero (it returns null, which you’re relying on). If the padding length is determined by a function then there’s no way to tell how big the result could be, apart from calculating it for every value in the result set before returning the metadata and and actual data, which isn’t practical, and would produce inconsistent output as the data changed.

    It also wouldn’t be practical to try to determine a theoretical maximum, even though it looks superficially straightforward in your case. substr can’t ever return something longer than the original value; but rpad could potentially produce something huge even from a short input value, so it has to allow for that possibility if it can’t easily determine a limit (i.e. from a fixed value).

    So it plays safe and allows for it being up to the maximum length for a varchar2, which is 4000 characters, as this dynamic SQL demonstrates:

    declare
        l_curid integer;
        l_desctab dbms_sql.desc_tab3;
        l_colcnt integer;
    begin
        l_curid := dbms_sql.open_cursor;
    
        dbms_sql.parse(l_curid, 'select rpad(ename, instr(ename,'' '')), '
            || 'rpad(ename, 4), '
            || 'substr(ename, 1, instr(ename,'' '')) '
            || 'from emp where ename like ''B%''' , dbms_sql.native);
    
        dbms_sql.describe_columns3(l_curid, l_colcnt, l_desctab);
    
        for i in 1 .. l_colcnt loop
            dbms_output.put_line('column ' || i
                || ' ' || l_desctab(i).col_name
                || ' type ' || l_desctab(i).col_type
                || ' length ' || l_desctab(i).col_max_len
            );
        end loop;
    
        dbms_sql.close_cursor(l_curid);
    end;
    /
    
    column 1 RPAD(ENAME,INSTR(ENAME,'')) type 1 length 4000
    column 2 RPAD(ENAME,4) type 1 length 16
    column 3 SUBSTR(ENAME,1,INSTR(ENAME,'')) type 1 length 40
    

    As you can see, it knows the length for a fixed-length rpad and a substr (note the size is four times the actual string length due to the multibyte characterset), but falls back to the maximum for the rpad using a function.

    What you’re seeing is SQL*Plus showing a 4000-char column. If you did this in SQL Developer you would see the header for that column is indeed 4000 characters. SQL*Plus helps a bit by reducing the displayed column header to the line size, and wraps the next column onto a separate line.

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

Sidebar

Related Questions

Using Oracle 11g I want a single exception to an otherwise sorted table select
I am developing an application using oracle 11g, Java(struts2) and Hibernate. I have table
I am using oracle 11g and have a table with an XMLType. There are
We are using Oracle 11g database with XMLDB installation. We are having table with
I'm using Oracle 11g R2. I have a table with 9.1 billion rows. When
I am using Oracle 11g r2 . I have a table that stores images
I'm using Oracle 11g, and trying to create a table define constraints on the
I'm using Oracle 11g Schema I want to select the month and day from
I'm using oracle 11g sql developer I have a varchar2 column with dates as
I am using Oracle 11g client, with ODP.NET. I am trying to add conditional

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.