Solved

Right Function in Oracle SQL Query

Posted on 2016-09-02
6
50 Views
Last Modified: 2016-09-02
I have a field in my database and the only thing I need to capture is everything except the last 3 characters of the field.
Ex.
FieldName                           Expected Result
EDITradepartners850        EDITradepartners
MMCCEDI850                      MMCCEDI

I tried using the left function, but since the names vary in length, I don't thing that's a good way to do it.

Any help would be appreciated.
0
Comment
Question by:metalteck
  • 3
  • 3
6 Comments
 
LVL 34

Expert Comment

by:johnsone
ID: 41782281
I would think it should be:

substr(fieldname, 1, length(fieldname)-3)

As far as I know, Oracle doesn't have a LEFT or a RIGHT function.  However, the function of those can be done with SUBSTR.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41782286
If you are trying to get the Oracle equivalent of the LEFT and RIGHT function, then these would be equivalent:

LEFT(FieldName, 10) = SUBSTR(FieldName, 1, 10)
RIGHT(FieldName, 3) = SUBSTR(FieldName, -3)
0
 

Author Comment

by:metalteck
ID: 41782295
Don't think that's working.

This is the code I'm using:
select edt.edi_nbr,
substr(edt.edi_nbr,1,length(edt.edi_nbr)-3) as TPID
FROM PROD.EDTRDPTNRS edt

The results:
3M850                           3M850
AAF850                          AAF850
ABIOM850                    ABIOM850
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 500 total points
ID: 41782301
Works for me:
WITH edtrdptnrs 
     AS (SELECT '3M850' edi_nbr 
         FROM   dual 
         UNION ALL 
         SELECT 'AAF850' edi_nbr 
         FROM   dual 
         UNION ALL 
         SELECT 'ABIOM850' edi_nbr 
         FROM   dual) 
SELECT edt.edi_nbr, 
       Substr(edt.edi_nbr, 1, Length(edt.edi_nbr) - 3) AS TPID 
FROM   prod.edtrdptnrs edt; 

Open in new window

Produces this result:
EDI_NBR  TPID
-------- --------------------------------
3M850    3M
AAF850   AAF
ABIOM850 ABIOM

Open in new window

If it isn't working on your end, what is the dataype of EDI_NBR?  Is it CHAR or VARCHAR?  If CHAR, then it is working and just removing the last 3 blank spaces.  You would have to change it to:

substr(edt.edi_nbr,1,length(rtrim(edt.edi_nbr))-3)
0
 

Author Comment

by:metalteck
ID: 41782316
Looks like I may have whitespace at the end.
I used both the trim and rtrim function, but here are the results I get.

EDI_NBR      TPID      TPID_2
3M850                3M850      M85
AAF850               AAF850      F85
ABIOM850             ABIOM850      M85
ACCLARENT850         ACCLARENT850      T85
ACRACUT850           ACRACUT850      T85
ACUDERM850           ACUDERM850      M85
0
 

Author Closing Comment

by:metalteck
ID: 41782319
Didn't see your comment on using the rtrim in the length.
I did that and it work.
Thanks for all the help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

744 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

10 Experts available now in Live!

Get 1:1 Help Now