I'm in a bit of a time crunch. I have a table with 2 columns. firstName, and lastName.
The data imported into SQL had both first and last name inside of the firstName column.
Is there a quick way to update the table to put everything before the first space into the lastName, and everything after the space into the firstName column? I know I could export, and do this via excel, but it in close to production time so I would rather not risk issues.
I have looked at a couple different split posts on here, but those do not involve updating at the same time.
Try to use:
UPDATE
table
SET
lastname = SUBSTRING(firstName, 1, CHARINDEX(' ', firstName) - 1),
firstName= SUBSTRING(firstName, CHARINDEX(' ', firstName) + 1, LEN(firstName))
MySQL
Demonstration:
SET #str := 'Robert Optional Dickey';
SELECT
SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1) AS lastName,
SUBSTRING_INDEX(#str,' ',-1) AS firstName;
Output:
lastName firstName
Robert Optional Dickey
Update Query:
UPDATE your_table
SET lastName = SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1),
firstName = SUBSTRING_INDEX(#str,' ',-1);
Note: It will work for any number spaces inside the full name.It just considers the string after the last space as first name.
Related
I have a NAME column and the data is stored like this in a MySql database:
Doe,John P
I would like to break it out into FNAME LNAME MNAME and FULLNAME.
If I don't want to tamper with the SQL table, should I create a view?
I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server.
This is what I have so far. I thinking I need a compound query or something similar.
SELECT Name
, SUBSTRING_INDEX(Name,',',-1) As FNAME
, SUBSTRING_INDEX(Name,',',1) As LNAME
, SUBSTRING_INDEX(Name,' ',1) As MNAME
FROM people;
I'm having trouble grabbing the middle name and doing what I think should be CONCAT function to get FULLNAME.
It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. (i.e. DOE,JOHN).
A view is a fine way to get the results you want for future queries. The defining query would be:
SELECT Name, SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ',', -1), ' ', 1) As FNAME,
SUBSTRING_INDEX(Name, ',', 1) As LNAME,
SUBSTRING_INDEX(Name, ' ', -1) As MNAME
FROM people;
This will work assuming that the format is exactly correct as you describe it in the table. Small changes -- such as no middle name or a space after the comma -- would make the syntax more complicated.
So the search would be like 'Ozzie Smith'
First table has (username, fname, lname) = osmith, ozzie, smith
Second table has (username, team) = osmith, cardinals
I need to concat the first and last name columns from the first table join it by username on the second table and return the team name.
I have been trying and trying...brain melted. I need your help!
Thanks!
Since it's MySQL, you need to use CONCAT, not the + sign. I also added a LOWER() function to avoid capital letter mismatch problem :
select team
from table1
join table2 on table2.username = table1.username
where lower(concat(fname,' ',lname)) like lower('%Ozzie Smith%')
You're probably doing something like
WHERE fname LIKE '%Ozzie Smith%'
which will not work. fname will contain only Ozzie, and will never match against Ozzie Smith. It would also not match if the user enters Smith, Ozzie, or any other dual-name variant. You'll have to preprocess the search terms and do something like
WHERE (fname LIKE '%Ozzie%') or (fname LIKE '%Smith%')
OR (lname LIKE '%ozzie%') or (lname LIKE %Smith%')
This will get VERY painful as the number of search terms and fields being search goes up. You'd be better off using a FULLTEXT index, where it'll be a simple matter of
WHERE MATCH(fname, lname) AGAINST ('Ozzie Smith')
Why doesn't this work?
select
lname + ', ' + fname as playerName
, team as teamName
from table1
join table2 on table2.username = table1.username
Update:
where (fname + ' ' + lname) like '%Ozzie Smith%'
I was wondering if you have two columns for lets say
first name.
last name.
what you store in the database.
Can you create a 'dynamic' column 'fullname' in the database that automatically creates the name out of the first and last name?
firstname |lastname |fullname (this goes automatically)
-----------------------------------------
foo |bar |foo bar
If you just want to select the full name, you can do just that:
select concat(firstname, ' ', lastname) as fullname from users
If you actually want a column (i.e. be able to perform updates against it), then it's not possible, AFAIK.
You can try something like this:
SELECT CONCAT(firstname, ' ', lastname) as firstlast FROM users
This one also makes sure that no empty spaces are added:
SELECT IF(ISNULL(firstname), lastname, CONCAT(firstname, ' ', lastname)) AS fullname FROM users
I have a MySQL table with over 3 million rows that includes peoples names and the state they are from e.g. "Smith (SC)" I would like to remove the state info and have "Smith". Since there are many difference last names and states used I was wondering if there was any trick to make this easier and just remove the parentheses and the text contained inside of them.
Any advice would be greatly appreciated.
Test with a select to make sure you're getting good results:
SELECT LastName, LEFT(LastName, LOCATE('(', LastName) - 1) AS CorrectedLastName
FROM YourTable
WHERE LOCATE('(', LastName) <> 0
Once you're confident in the results:
UPDATE YourTable
SET LastName = LEFT(LastName, LOCATE('(', LastName) - 1)
WHERE LOCATE('(', LastName) <> 0
Make a backup first, then try
Update <tablename>
set name =substr(name,1, instr(name,'('));
substr is for oracle but check the mysql equivalent
I have a column named streetaddress that contains
<Street Number> <Street Name>
for example:
15 rue Gontier-Patin
4968 Hillcrest Circle
how can i remove the numbers from the beginning of the row purely in sql?
How about something like this - trim off everything up to and including the first space in strings which start with a number
UPDATE mytable
SET addresscol=SUBSTRING(addresscol, LOCATE(' ', addresscol)+1)
WHERE addresscol REGEXP '^[0-9]';
This is based on #Paul Dixon above but is just for returning results without updating the data for things like sorting:
SELECT IF(address REGEXP '^[0-9]', SUBSTRING(address, LOCATE(' ', address)+1), address) AS addrSort FROM table;
MySQL does not have regexp replace functions so it depends on the values in your column. If the string always begins with a number and then a space, you can do it with the SQl query Paul Dixon posted (doh, he was faster than me :D ).
If you need regular expressions to solve it, you have to do it in application code outside the database.
I think this should do the job on the basis that each entry has it's street address seperated from the house number by a space (" ");
UPDATE table
SET streetaddress = MID(streetaddress, LOCATE(' ', streetaddress) + 1);
I've tested this and it works fine.
You can also use the following if you'd like to extract the house number to a new column previous to the update;
UPDATE table
SET housenumber = MID(streetaddress, 1, LOCATE(' ', streetaddress) - 1);