Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mysql subquery (query for function parameter)

Posted on 2014-01-10
2
Medium Priority
?
279 Views
Last Modified: 2014-02-11
Not sure if this is ok to combine so many things...

If I have a table with columns:
firstname
lastname

the name "experts exchange" in firstname.

I want to search for the space, and put everything after into `lastname`. ie(firstname:experts  lastname:exchange)

Logically, this is what I would want.
update table set `lastname` = substr(`firstname`, (SELECT instr(`firstname`, ' ') FROM table));

(even that though would include the space, so I would want instr + 1. Or I could do a second pass to remove the leading space afterwards....

Thanks in advance!
0
Comment
Question by:ststesting
[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
2 Comments
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 1000 total points
ID: 39772649
You could use SUBSTRING_INDEX(lastname, " ", -1) to find the last space.

So then you would select  SUBSTRING(lastname, SUBSTRING_INDEX(lastname," ",-1)+1) to get everything following the last space.
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 39773089
You can try this.
Update test
Set lastname = trim(substring_index(firstname,' ',-1));

Open in new window


http://sqlfiddle.com/#!9/785c3/1
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

705 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