MYSQL extract uppercase words - mysql

I am trying to extract words which are in uppercase and lowercase.
Example, in single column(Full_name) having first and last name values, here I need to separate them as first and last names, first names always have in initial capital letter and last names have always all letters in uppercase.
Full_Name:
---------
abc ABC
pqr RTF
now I need separate them in 2 different columns as below
First_name Last_name
--------- --------
abc ABC
pqr RTF
Thank you very much for you inputs in advance.

SELECT LEFT(Full_Name,LOCATE(' ',Full_Name)) AS First_name,
SUBSTRING(Full_Name,LOCATE(' ',Full_Name)) AS Last_name
FROM TABLE_NAME
Check SQL Fiddle

The easiest way in MySQL is to use substring_index():
select substring_index(full_name, ' ', 1) as First_Name,
substring_index(full_name, ' ', -1) as Last_Name
from table t;
EDIT:
I missed the names with multiple parts.
select substring_index(full_name, ' ', 1) as First_Name,
trim(substring(full_name,
length(substring_index(full_name, ' ', 1))
)
) as Last_Name
from table t;

Related

How do i get the first name from the customer_name column

select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;
This solution is giving me the answer but It doesn't work with the last name
Please test this: I used locate function to define the position of ' '.
SELECT
LEFT(customer_name, LOCATE(' ',customer_name)-1) as first_name,
RIGHT(customer_name, LENGTH(customer_name)-LOCATE(' ',customer_name)) as last_name
FROM customer;
Result set:
Use SUBSTRING_INDEX() it takes 3 parameter:
Column name
Delimiter
Occurrence count
You can find more explanation in this article
Query
SELECT
SUBSTRING_INDEX(customer_name,' ', 1) as first_name,
SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;

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:]]+', '');

Search in Full name column with first and last name

I want to achieve is to search for a full name in a database with only a string with his first and last name.
in database there is the full name:
Manuel Augusto Vieira Conde Fialho
and I want a sql query that can search this full name only with the first and last name :
Manuel Fialho
I've tried this one:
SELECT name FROM partners WHERE name LIKE 'Manuel Fialho'";
but this does not work for me because I don't have any Manuel Fialho in the database only a Manuel Augusto Vieira Conde Fialho and because of that the name im looking for does not show.
thank you
You shoould add %:
SELECT name FROM partners WHERE name LIKE 'Manuel%Fialho';
One method uses wildcards:
SELECT name
FROM partners
WHERE name LIKE REPLACE('Manuel Fialho', ' ', '%');
SELECT name FROM partners WHERE name LIKE "Manuel%" OR name LIKE "%Fialho";
If You are sure about the last name of the name then you can also use AND
SELECT name FROM partners WHERE name LIKE "Manuel%" AND name LIKE "%Fialho";
There are 2 possible options to search.
Split search string into multiple words and query with that many LIKE patterns.
select name FROM partners WHERE name LIKE '%Manuel%' or name like '%Fialho%'
If you are sure that the sequence of words in the search strings are to match with those in the column value then replace all spaces with % symbol and use with LIKE.
select name FROM partners
WHERE name LIKE concat( '%', replace( 'Manuel Fialho', ' ', '%' ), '%' )
Try Below Query:
It works fine..!
SELECT * FROM `partners` WHERE
`name` LIKE replace( 'Manuel Fialho', ' ', '%' ) OR
`name` LIKE replace('%Manuel Fialho%', ' ', '%' ) OR
`name` LIKE replace('%Manuel Fialho', ' ', '' ) OR
`name` LIKE replace('%Manuel Fialho%', ' ', '' );

mysql query where second words starts with a letter

I have a table with:
Name Surname and Outputname ( = Name Surname )
Name and Surname should always have a value, but some time are empty.
I need to select from table by surname and i can't use surname field.
I can't even change field values with a script because i get this table from an outside source and can be change any time.
Database is MySQL 4.x
Can i select by second word in Outputname starting with some letter?
something like
SELECT Outputname FROM USERS
WHERE seconword(Outputname) LIKE 'A%' SORT BY seconword(Outputname) ASC
try this
SELECT
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
WHERE SUBSTRING_INDEX(Outputname, ' ', -1) LIKE 'A%'
ORDER BY SecondWord ASC
demo
One possible approach:
SELECT Surname
FROM (
SELECT SUBSTRING_INDEX(Outputname, ' ', -1)
AS Surname
FROM Users) AS S
WHERE Surname LIKE 'A%'
ORDER BY Surname;
SQL Fiddle. This method is based on assumption that Outputname's format is always 'FirstName LastName' (i.e., ' ' symbol is used as a delimiter, and used only once each time).
I didn't understand your question at all. So you only have access to Outputname (composed by two words name+surname) and you have to sort it by the second word right?
Try something like (it worked for me):
SELECT
Outputname,
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
ORDER BY SecondWord ASC
Clause SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord returns the last word placed after a space. So If you have Outputname = 'Maria Callas' it returns 'Callas', if you have Outputname = 'Sophia Cecilia Kalos' it returns 'Kalos'.

searching with a name in database for a person - MySQL

In my table I store first name in the fName column and last name in the lName column, now I need to search them with a query, but I don't know the SQL!
example
lName | fName
-----------------
Tendulkar| Sachin
Ganguly | Sourav
Khan | Zaheer
Dhoni | Mahendra Singh
The user should get MAHENDRA SINGH DHONI if he searches for Mahendra Dhoni!
select concat(fName,' ',lName) fullname
from tbl
where concat(' ',fName,' ',lName,' ') like '% Mahendra %'
and concat(' ',fName,' ',lName,' ') like '% dhoni %'
This will most certainly put to rest any hopes of a well performing query
A variation on the theme
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
or concat(lName,' ',fName) like '%Mahendra%dhoni%')
The 2nd version doesn't care about full part matching, e.g. dhoni will match madhonie
Both of these queries find the name correctly. Note that there are % before and after the name to match, as well as % for every space in the name.
create table tbl (fname varchar(100), lname varchar(100));
insert tbl select 'Mahendra singh', 'dhoni';
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
or concat(lName,' ',fName) like '%Mahendra%dhoni%');
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%dhoni%Mahendra%'
or concat(lName,' ',fName) like '%dhoni%Mahendra%');
You are not clear on the nature of the search inputs and specifically the level of flexibility. First, is the user given two boxes for first and last name or only a single search box? If the former, then the fast solution would be:
Select concat( fname, ' ', lname)
From MyTable
Where lname Like 'dhoni%'
And fname Like 'mahendra%'
The above query only searches for where the first part of the column value begins with the search values. However, if the user can type anything into a single search box, that is harder. If it is presumed that the user has typed <name part> space <name part>, then one solution that solves that specific problem where the user enters only two words is to split on the space and run something like:
Select concat( fname, ' ', lname)
From MyTable
Where ( lname Like '%dhoni%' And fname Like '%mahendra%' )
Or ( lname Like '%mahendra%' And fname Like '%dhoni%' )
However, that query will perform awful because it forces the system to scan the entire table each time it is executed. Further, what happens when they enter a three part name like Mahendra Singh Dhoni in your search? There are simply too many edge cases for this to be workable IMO. The right solution is to get a full text indexing engine like Lucene that will create a index across both columns and rank the quality of the match.
Lucene
I'm guessing your inputs are from two different textboxes, and hopefully you are using a stored procedure :).
declare #input1 nvarchar(50)
declare #input2 nvarchar(5)
set #input1 = 'Mahendra'
set #input2 = 'Dhoni'
select upper((fname + ' ' + lname)) as 'FullName'
from customer
where fname like '%' + #input1 + '%'
or fname like '%' + #input1 + '%'
or lname like '%' + #input2 + '%'
or lname like '%' + #input2 + '%'
If you have 1 input box, you will want to split your search term by the space and loop over your search terms against fname and lname columns using the like '%term%' syntax.
Another way would be to make a stored procedure that added fname and lname together and did a soundex match. this is where you match your search term against the sound of the word in the table. Google it should help, its pretty easy.
I am assuming you have 2 inputs and you are only getting part of the first name. If this is true then the following would work:
select concat(fname, ' ', lname)
from yourtable
where substring(fname, 1, 5) = 'mahen'
and lname = 'dhoni'
The advantage of this approach is that it will use the indexes on the column(s) or a combined index whereas the like queries always do full table scans.
You might also check out a search engine like sphinx or solr when you truly do not know the data you are receiving as those are far more flexible that database queries.