SQL’s LEFT, RIGHT, CHARIndex – Oracle Equivalents

If for no one else, I wanted to post this somewhere for my own reference.  Having used SQL server during my “formative years” I find myself writing queries in Oracle and cursing aloud when my old ways of doing things don’t work.  Here is one that I can’t seem to commit to memory.

If I wanted to grab the left portion of some data in SQL Server I would do the following:
select LEFT(theField,10) from theTable

If I wanted to grab the left portion of the data that preceded some character like the ‘@’ in an email address I would do something like the following:
select LEFT(theField,charIndex(theField,’@')-1)

For reference, here are the Oracle Equivalents:

To grab the left 10 characters:
select SUBSTR(theField,1,10)

To grab the right 10 characters:
select SUBSTR(theField,-10)

To grab the characters to the left of the ‘@’ sign like above:
select SUBSTR(theField,1,instr(theField,’@')-1)

  1. #1 by Dan - November 17th, 2010 at 12:16

    thank you. This helped save me a ton of time searching.

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.
SetPageWidth