mysql query to remove space after last comma in string - mysql

I have a mysql table with data like this.
Fullname
Adarna , Neil.
David , Jundemil.
I want to remove all spaces between lastname and the comma
so the data looks like this.
Adarna, Neil.
David, Jundemil.
tried this query
SELECT SUBSTRING_INDEX(`fullname`, ' ,', 2) from tbl_users
output was all fullname with commas

You can try as per below-
update mytable set fullname = replace(fullname,' ,',',');

Related

MySQL WHERE IN (substrings of a string)?

So I am trying to do this:
SELECT customerID, name, email
FROM Customers
WHERE customerID IN (substrings delineated by spaces of CID string)
Where what I want to happen, is it returns the entries that have a matching customerID.
Unfortunately, the value CID is a string of all the matching CIDs separated by spaces. I've tried to change it to be some sort of list, but no luck on that end. If I could use something like string splitter that would solve my problem, but it doesn't seem to be available in MySQL.
Sorry for the relatively simple question, but an hour of googling has returned no clear answers.
I wonder if you could use the REPLACE function:
SELECT customerID, name, email
FROM Customers
WHERE customerID IN (REPLACE(CID string,' ',','))
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_replace
One method is like:
SELECT customerID, name, email
FROM Customers
WHERE CONCAT(' ', CID, ' ') LIKE concat('% ', customerID, ' %') ;
I really do advocate that you separate the string. If it is coming from a table, then you probably need a junction/association table.
Try this:
WHERE FIND_IN_SET(CustomerID, REPLACE(spacedList, ' ', ',')) > 0
FIND_IN_SET
You might need to also cast CustomerID to a char, but I would try without the cast first.

remove whitespace in between Column values in mySQL

How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');

Update that splits one column into two?

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.

search box -> I need to concat first name and last name from one table and join it to a 2nd and return item from 2nd table

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%'

Apostrophe replacement in mysql query

I have in a column names(notes) as
Notes:
John's Table
Smith's Window
Column contains an apostrophe in it. I am trying to run 2 queries.
Select query to get the column entries that have apostrophe.
Update those with apostrophe with empty string ie.John's Table replace to John Table
select notes from table1 where notes like ' '% ';
I get a syntax error , any help will be great.
Escape the apostrophe with a backslash;
select notes from table1 where notes like '%\'%';
And to update, use REPLACE()
UPDATE table1 SET notes = REPLACE(notes, '\'', '');
Did you try:
select notes from table1 where notes like "'%";