Solved

Select partial string

Posted on 2014-09-17
9
186 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
  • 6
  • 2
9 Comments
 
LVL 1

Accepted Solution

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

Author Comment

by:hdcowboyaz
Comment Utility
Perfect

How would I return the First name after the semicolon

 Jason
Edward
0
 
LVL 2

Expert Comment

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

Open in new window

0
 
LVL 1

Expert Comment

by:njp353
Comment Utility
SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(ENAME, ';', 2)),' ',1) AS FNAME FROM EMP;
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:hdcowboyaz
Comment Utility
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
Comment Utility
I got it...thanks...

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

Author Comment

by:hdcowboyaz
Comment Utility
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
Comment Utility
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
Comment Utility
Got it...

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now