Solved

Left Justify field in Oracle

Posted on 2016-09-12
6
68 Views
Last Modified: 2016-09-15
I'm using the following code to gather the information.
||rpad(substr(emp.addr1,1,40),40)

Problem is when I export it, the data comes like this.
How can I get it so that its Left justified with no spaces?
              10211 Anywhere Street        

Thanks
0
Comment
Question by:metalteck
  • 3
  • 2
6 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
if you don't want it to be padded with spaces, don't pad it.
you can add a TRIM call to make sure you remove any leading or trailing spaces that are already in the data

trim(substr(emp.addr1,1,40))
0
 
LVL 34

Expert Comment

by:johnsone
Comment Utility
If there are leading spaces in the database field and you don't want those to count in your substring length, the the trim would have to go first.

||rpad(substr(ltrim(emp.addr1),1,40),40)
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 250 total points
Comment Utility
Still not sure why you're using substr if emp.addr1 is char(30). Just ||rpad(trim(emp.addr1),40, ' ')
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Accepted Solution

by:
johnsone earned 250 total points
Comment Utility
Why are you using TRIM and not LTRIM?  You are removing spaces from the right (if they are there), only to put them back with RPAD.  Seems like wasted cycles to me.
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Good point, johnsone. From a related question emp.addr1 was indicated as a char of fixed length 30. The 10211 Anywhere Street  example would be those 21 characters followed by nine spaces. So I think all that should be needed is simply ||rpad(emp.addr1,40) to make it left-justified.
0
 
LVL 34

Expert Comment

by:johnsone
Comment Utility
The LTRIM is needed.  As requested you need to get rid of leading spaces.  TRIM isn't needed because you are padding on the right anyway, so there is no need to waste the cycles to remove the trailing spaces (you are just going to add them back anyway).  At least the sample data in the original question has leading spaces.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now