Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > How to use SQL's POSITION function with substrings
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

How to use SQL's POSITION function with substrings

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 13 January 2009

I have a column in one table which contains data like this:

SE.108.000.034MFJ
SE.108.000.034MFJ-0LNC
SE.108.001.023LIN-1PBA.NNA
SE.108.050.043ERTO-0FTA
SE.045.000.011EMFJ

I would like to extract everything from the left until the "-" character. I have tried (SUBSTR(p1.location,1, INSTR(p1.location,'-')-1)) but the problem is that INSTR returns "0" if no "-" are found, and then the SUBSTR is empty. What other possibilities do I have?



Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?
Using the SQL date function to find aggregate totals by month

Oracle and SQL
Oracle tutorial library: SearchOracle.com's learning guides
Can I specify Oracle column order in my database table?
Review: Oracle's 11g R2 database has some good and bad
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
Stored procedures in PL/SQL
PL/SQL functions and triggers in Oracle
Do I need a license for SQL Developer Data Modeler in Oracle?
Using the SQL GROUP BY clause for counting combinations
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


Here's a really simple "trick" you can use: just concatenate a dash onto the end of the column when searching for the position of the first dash.

SELECT SUBSTRING(
          p1.location 
          FROM 1
          FOR POSITION('-' IN p1.location||'-') - 1
                )
  FROM daTable AS p1

In this query, the SUBSTRING and POSITION functions are the standard SQL versions of these functions. These functions behave just as you would expect. Your particular database system may have slightly different names and syntax for these functions, but they will work the same way.

When the column value contains a dash, you get the expected result. For example, consider the value:

SE.108.050.043ERTO-0FTA

This value is 23 characters long, and contains a dash at position 19. When we concatenate the extra dash to the end of this value, it becomes:

SE.108.050.043ERTO-0FTA-

However, the POSITION function will find the first dash at position 19. Then we subtract 1 from this position number, giving 18, and use this as the length of the substring to be extracted:

SE.108.050.043ERTO

Now let's consider the case where the column value contains no dash, such as:

SE.045.000.011EMFJ

This value is 18 characters long. When we concatenate the dash to the end of this value, it becomes:

SE.108.050.043EMFJ-

The POSITION function will find the extra dash which we concatenated to the end, at position 19. Subtracting 1 gives 18, which just happens to be the length of the original column value. So in this case, the SUBSTRING function returns the entire value.

Neat, eh?




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts