Job Seekers   Employers
dbapool: Authors | Submissions | Contact Us
   Forgot password? | Sign up
  Home   Discussion Forum   Articles   Interview Questions   FAQs   Scripts   Rewards   Analyzer   White Papers   Blog   Certification   Downloads   Tools
 

Topic: SQL*Plus and PL/SQL >> SUBSTR

Disclaimer: The purpose of all dbapool forums including OCP and Other Oracle Certification forums is to help each other with specific issues but not to share dump and copyrighted exam content, materials or intellectual property. You may review the entire Oracle Certification Program Candidate Agreement online Here.



  


 Title: SUBSTR
 Muks  Posted: Jul 13, 2007 07:42:57 AM

 Total Post: 47
 Joined: Jan, 2007






 
I have a column Company..which has been populated like eg.

Company
-------------------
TATA Cons Services
Wipro Chems Fertilizers
Infy Tech Services

Now I want to split company into three columns...as first middle and last name eg for first row
First is TATA
Middle is Cons
Last is Services

how wud i do that in sql









 Nand
Posted: Jul 13, 2007 09:17:35 AM  

 Total Post: 22
 Joined: Jun, 2007






 
Hi Mukta,

This is the query:

select substr(company, 1,(instr(company, ' '))) "first" , substr(company, (instr(company, ' ' )),(instr(company, ' ',2))) "second" , substr(company,instr(company, ' ',-1)) "third" from tablename;

Regards
Nand

 Vijay
Posted: Jul 16, 2007 12:23:22 AM  

 Total Post: 12
 Joined: Jul, 2007






 
Dear Nand,
Your query :
select substr(company, 1,(instr(company, ' '))) "first" , substr(company, (instr(company, ' ' )),(instr(company, ' ',2))) "second" , substr(company,instr(company, ' ',-1)) "third" from tablename;

will give following result:
first second third
TATA Cons Services
Wipro Chems Fertilizers
Infy Tech Services

Which coincidentally works finewith the data which we have at present in the table tablename.

The syntax for instr function works as follows:
instr(String,substring,position,occurence).

In the example discussed here, incidently data at the first and second position had equal number of characters. Hence you got it working.
Had te number of characters been different for first and second name the query would have incorrect result.

Just try out your query with following data in the table:
TATA Consultancy Services
Wipro Chems Fertilizers
Infy Tech Services

The correct query should have been:
select substr(company, 1,(instr(company, ' '))) "first",
substr(company, (instr(company, ' ' )),(instr(company, ' ',1,2))-(instr(company, ' ' ))) "second",
substr(company,instr(company, ' ',-1)) "third"
from tablename

Reards,
Vijay Mahawar.

 Nand
Posted: Jul 16, 2007 01:53:21 AM  

 Total Post: 22
 Joined: Jun, 2007






 
Vijay...

Thanks for correcting the error.

Regards
Nand

 Muks
Posted: Jul 16, 2007 06:33:03 AM  

 Total Post: 47
 Joined: Jan, 2007






 
Thank you Vijay and Nanda...

Besties...
Mukta

 Vijay
Posted: Jul 17, 2007 12:45:27 AM  

 Total Post: 12
 Joined: Jul, 2007






 
Hi Mukta/Nand,
Welcome..It was my pleasure..
Regards.
Vijay Mahawar.

 Nand
Posted: Jul 17, 2007 05:25:57 AM  

 Total Post: 22
 Joined: Jun, 2007






 
I feel the same...

Regards
Nand

 Time Zone: EDT

  




Forum Rules & Description


Who Can Read The Forum? Any registered user or guest
Who Can Post New Topics? Any registered user
Who Can Post Replies? Any registered user




 








Get FREE Magazines

Top 10 Forum User

  Murtuja Khokhar857
  Mohammed Taj746
  positive fanatic483
  Jayanta Sur479
  Vigyan Kaushik386
  Gopu Gopi333
  Gitesh Trivedi322
  Vinoth Kumar264
  neeraj sharma258
  Ramesh Jois246






oracle Mag



  About Us Advertise Terms of Use Privacy Newsletters Contact Us    

Home   Discussion Forum   FAQs  Articles  Jobs   Newsletters  Directory  Downloads 

Our Premium Sponsor
Confio Software