Home > Ask the Oracle Experts > Questions & Answers
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Joining on a substring of a column

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 10 March 2005

Using MS Access:

SELECT * FROM EEqry E, LoanOfficers L 
WHERE E.FirstName = L.FirstName

How can I change this SQL statement so that both FirstName fields only compare the value up to the first space? What I'm try to accomplish is if E.FirstName = "John T." and L.FirstName = "John" or E.FirstName = "John T." and L.FirstName = "John C." they both would be equal because they would only be comparing the string, "John".


>
EXPERT RESPONSE

In standard SQL, this is accomplished using string functions:

select * 
  from EEqry E
inner
  join LoanOfficers L 
    on case when position(' ' in E.Firstname) = 0
          then E.FirstName
          else substring(E.FirstName from 1 
                for position(' ' in E.Firstname)-1)
        end           
     = case when position(' ' in L.Firstname) = 0
          then L.FirstName
          else substring(L.FirstName from 1 
                for position(' ' in L.Firstname)-1)
        end

Notice how a CASE expression is used to ensure that if the name does not actually contain a space, then the comparison will use the whole name.

In Access syntax, this becomes:

select * 
  from EEqry E
inner
  join LoanOfficers L 
    on iif(instr(E.Firstname,' ') = 0
            , E.FirstName
            , left(E.FirstName
                 , instr(E.Firstname,' ')-1)
          )           
     = iif(instr(L.Firstname,' ') = 0
            , L.FirstName
            , left(L.FirstName
                 , instr(L.Firstname,' ')-1)
          )

Some people dislike Microsoft Access because its version of SQL is [quote]so[unquote] non-standard, but honestly, it's not that bad once you get to know it!


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


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



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

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

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




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