Solved

Select partial string

Posted on 2014-09-17
9
190 Views
Last Modified: 2014-09-17
I want to return only part of the string up to the semicolon. In the fields below I want only "Abbott" and "Marshall" returned.

select ename from emp;

Ename
Abbott; Jason Clark George
Marshall; Edward Henry
0
Comment
Question by:hdcowboyaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 1

Accepted Solution

by:
njp353 earned 500 total points
ID: 40329663
SELECT SUBSTRING_INDEX(ENAME, ';', 1) AS LNAME FROM EMP;
0
 

Author Comment

by:hdcowboyaz
ID: 40329673
Perfect

How would I return the First name after the semicolon

 Jason
Edward
0
 
LVL 2

Expert Comment

by:Akilandeshwari N
ID: 40329682
Try this.
SELECT TRIM(LEADING ';' FROM 'Abbott; Jason Clark George');

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Expert Comment

by:njp353
ID: 40329685
SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', 2)),' ',1) AS FNAME FROM EMP;
0
 

Author Comment

by:hdcowboyaz
ID: 40329694
That returned the Last name followed by the semicolon  
SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', 2)),' ',1) AS EFIRST FROM emp;
Abbott;

This returned the whole name
SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', 2)),' ',3) AS EFIRST FROM emp;
Abbott; Jason Clark George
0
 

Author Comment

by:hdcowboyaz
ID: 40329700
I got it...thanks...

SELECT SUBSTRING_INDEX(ENAME, ';', -1) AS EFIRST FROM emp;
0
 

Author Comment

by:hdcowboyaz
ID: 40329706
Actually it returns everything after the semicolon, instead of the first name only/\.

Ename
Abbott; Jason Clark George
Marshall; Edward Henry

Jason Clark George
Edward Henry
0
 

Author Comment

by:hdcowboyaz
ID: 40329712
This return the last name in the string
SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', 2)),' ',-1) AS EFIRST FROM emp;

George
Henry
0
 

Author Comment

by:hdcowboyaz
ID: 40329717
Got it...

SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', -1)),' ',1) AS EFIRST FROM emp;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question