Solved

mysql subquery (query for function parameter)

Posted on 2014-01-10
2
267 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 250 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 250 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
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…

738 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