mysql query where second words starts with a letter - mysql

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

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;

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 how to have only first letter of each word in a column

In MySQL, I have StudLastName column and StudFirstName column
I want to query to get result like this (example with Johnny Cage):
Cage, J
My query looks like that and the first character of the first letter of every word in my FirstName Column are '0' so every name looks like: Cage, 0:
SELECT concat (StudLastName, ', ', StudFirstName like ' ') as 'Student Name'
Check https://dev.mysql.com/doc/refman/5.5/en/string-functions.html
SELECT CONCAT(StudLastName, ', ', LEFT(StudFirstName,1)) as 'Student Name' FROM yourtable

omit results starting with certain character

I am trying to retrieve a list of names from a table where the surname does not start with z or Z using mysql. I tried combining substring with instr to accomplish this. Attempt below:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'z'
OR SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'Z'
ORDER BY SName
My attempt is returning results with z as the first letter of the surname. Can anyone explain why? Or if there is a better way to achieve this
This can be much shortened with LIKE:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName NOT LIKE 'z%' AND FName NOT LIKE 'Z%';
IIRC LIKE is case-sensitive since MySQL v5.6.x
I wouldn't write it like
...WHERE LOWER(FName) NOT LIKE 'z%';
since applying functions on columns prevent MySQL from using the index on the column (if one exists).
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName REGEXP '^[^z]';

How to separate a comma separated name field in MySQL in to first name, middle name, last name and fullname?

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.