?
Solved

Select partial string

Posted on 2014-09-17
9
Medium Priority
?
194 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 2000 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

719 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