Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

Right Function in Oracle SQL Query

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
metalteck
Asked:
metalteck
  • 3
  • 3
1 Solution
 
johnsoneSenior Oracle DBACommented:
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
 
johnsoneSenior Oracle DBACommented:
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
 
metalteckAuthor Commented:
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
johnsoneSenior Oracle DBACommented:
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
 
metalteckAuthor Commented:
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
 
metalteckAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now