What MySQL LIKE wildcard should I use to return someone's name without their title/prefix name? - sql

I want to return name of customers and order them, but their titles must be excluded in ordering.
SELECT name
FROM customers
WHERE name LIKE ...
ORDER BY name
I mean by 'their titles' is such as Dr., Sn., Lady, Sir, Mr., and Mrs.

Possible solution to your problem.
In Oracle:
regexp_replace(user_name, '^(MISS|MS\.|MS|MRS\.|MRS|MR\.|MR)\s*', '') as user_name
Also you can use REPLACE () function like:
REPLACE (user_name, 'MISS', '') as user_name
If you have a column structure like (mr | mrs | other) / space / username you can try this:
with users(user_name) as
(select 'mr user name1' from dual union all
select 'miss username2 ' from dual union all
select 'other username 3' from dual )
select substr(user_name,instr(user_name,' ')+1) real_username from users
Output:
REAL_USERNAME
----------------
username 1
username 2
username 3
In MSSQL:
DECLARE #str VARCHAR(500)='Mr Sam'
SELECT Title,
first_name,
Substring(NAME, CASE
WHEN Charindex(' ', NAME) = 0 THEN 1
ELSE Charindex(' ', NAME)
END, Len(NAME)) last_name
FROM (SELECT CASE
WHEN LEFT(#str, Charindex(' ', #str)) IN( 'Mr', 'Mrs', 'Miss' ) THEN LEFT(#str, Charindex(' ', #str))
ELSE ''
END AS Title,
CASE
WHEN LEFT(#str, Charindex(' ', #str)) IN ( 'Mr', 'Mrs', 'Miss' ) THEN LEFT(Stuff(#str, 1, Charindex(' ', #str), ''), Charindex(' ', Stuff(#str, 1, Charindex(' ', #str), '')))
ELSE LEFT(#str, Charindex(' ', #str))
END AS first_name,
CASE
WHEN LEFT(#str, Charindex(' ', #str)) IN ( 'Mr', 'Mrs', 'Miss' ) THEN Stuff(#str, 1, Charindex(' ', #str), '')
ELSE #str
END NAME) a

Technically using a % before their name will allow any prefix, which would get you the desired result (ex. WHERE name LIKE ('%' + #name). However, this is not the recommended approach on larger data sets as you will see significant performance issues with this approach.

You need to be more specific. First of all: Which SQL "flavor" are you using? Postgres? Oracle? MySql? This is very important because each engine has different functions. Every time you ask an SQL question in SO, be sure to include at the very least a tag mentioning which DBMS you're using.
Now, what do you mean by "I shouldn't return them". Do you mean you should not return records which have a prefix, or do you mean you need to return the records but without the prefix? (So if you have Dr. Henry Gutierrez, do you exclude him from the result? Or have it output as Henry Gutierrez?)
This is also a good place for another tip: Always write an expected output in your questions. "If X is Y then I expect the output to be this:"
If you need to exclude them entirely, you can use a REGEXP match (Once again, I cannot list a specific function because I have no idea which type of SQL you're using) Something like WHERE REGEXP(UPPER(COL)) NOT ('^(MS|MR)\s+.*$')
If it's the 2nd case, that's going to be much harder because you'd need to get a substring which excludes the prefix, but the prefixes all have different sizes so you can't just write a "one size fits all"
In general, it's a bad normalization practice to have prefixes in your SQL database. You should have a column called PREFIX and another column for the name itself.
EDIT: Based on your answer. This is the closest you can get to achieving what you want.
SELECT NAME
FROM (SELECT NAME,
CASE WHEN NAME LIKE 'MR %' THEN SUBSTRING(NAME, 4)
WHEN NAME LIKE 'MRS %' THEN SUBSTRING(NAME, 5)
ELSE NAME AS NAME2
FROM YOUR_TABLE ORDER BY NAME2) AS SUBQ
I am 99% sure this will not return the results ordered though, because the select order might be ignored outside of the subquery, so you can also just try whats in the subquery in a main query instead, but this will output 2 columns.
***My reply is in pseudocode. It is not exactly MySql syntax, you will need to check MySql documentation for the actual substring function implementation it has and for the CASE syntax.

Related

RIGHT, LEFT and CHARINDEX not performing as expected

I am trying to reorder a column of names that is in the form lastname, firstname to firstname lastname. The original column is NAME_BAT and I have confirmed there are no leading or trailing spaces. Here is my SQL code:
SELECT TOP (100) NAME_BAT
, LTRIM(RIGHT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1)) AS FIRST_NAME
, RTRIM(LEFT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1)) AS LAST_NAME
, LTRIM(RIGHT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1)) + ' ' + RTRIM(LEFT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1)) AS NAME_FULL
FROM pitch_aggregate
;
and here is a screenshot of the output:
Output from SQL query
Why are the first four rows perfect, then row 5 includes a leading comma and space before the name, and in row 8 the first name has the first two letter cut off?
Take
LTRIM(RIGHT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1)) AS FIRST_NAME
Presume data like Casanova, Bob
CHARINDEX(', ', NAME_BAT) returns 9
RIGHT(NAME_BAT, CHARINDEX(', ', NAME_BAT) - 1) is the rightmost 9-1 = 8 characters, which is nova, Bob
(All your sample/testing firs and last names are nearly the same length.) I'm guessing this is not what you really want?
Now, something like
SUBSTRING(NAME_BAT, CHARINDEX(', ', NAME_BAT) + 2, 100)
might be what you're looking for. Me, I see it as a prime example of why SQL is a poor place to implement string manipulations.
I have tested your query and you do indeed have some formatting issues.
Rather than pick it apart, here is a simpler working alternative you can try. By using cross apply and a values construct you can abstract out the string splitting to leave a more easy to use select list of columns.
select name_bat, f.name FirstName, l.name LastName, Concat_ws(' ',f.name,l.name) Name_full
from t
cross apply (values( Left(name_bat,CharIndex(',',name_bat)-1 )))l(name)
cross apply (values( Replace(name_bat,Concat(l.name,', '),'') ))f(name)
See original and new version example

Separate fullname into first and last, and remove 'junk'

Wasn't sure of the best way to word this. So I have a column with names, as below:
SalesPerson_Name
----------------
Undefined - 0
Sam Brett-sbrett
Kelly Roberts-kroberts
Michael Paramore-mparamore
Alivia Lawler-alawler
Ryan Hooker-rhooker
Heather Alford-halford
Cassandra Blegen-cblegen
JD Holland-jholland
Vendor Accounts-VENDOR
Other Accounts-OTHER
Getting the names separated is easy enough with PARSENAME and REPLACE functions, but where I'm running into a pickle is with getting rid of the 'junk' at the end:
SELECT SalesPerson_Key
,SalesPerson_Name
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2)
END AS FirstName
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN NULL
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
END AS LastName
FROM Salesperson
RESULTS FOR LASTNAME COLUMN:
LastName
--------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler
Hooker-rhooker
Alford-halford
Blegen-cblegen
Holland-jholland
Accounts-VENDOR
Accounts-OTHER
Specifically, I want to get rid of the text (userid) at the end of the last name. If the names were the same length, I could just use a RIGHT function, but they vary in length. Ideas?
select left(PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1), len(SalesPerson_Name)-CHARINDEX('-',SalesPerson_Name)-1)
You are getting charindex of - and taking the left string of it.
If you just want to remove the last word (username) you can use a query like this
select
rtrim(
substring(
SalesPerson_Name,
1,
charindex('-',SalesPerson_Name,1)-1
)
)
from Salesperson
The charindex function locates the occurrence of the character/s you are looking for.
Consider whether hyphen is followed by a space or not, and split depending on these two cases
with Salesperson( SalesPerson_Name ) as
(
select 'Undefined - 0' union all
select 'Sam Brett-sbrett' union all
select 'Kelly Roberts-kroberts' union all
select 'Michael Paramore-mparamore' union all
select 'Alivia Lawler-alawler'
)
select case when substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,1) = '-' then
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+3,len(SalesPerson_Name))
else
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,len(SalesPerson_Name))
end as last_name
from Salesperson s;
last_name
------------------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler

SQL Returning names with multiple spaces

I am looking to return all names with more than one space in a single field.
For example 'John Paul Smith'. Using SQL server management studio 2005
Example I have a patients table with forename and surname
I want to return all forenames that have example 'John Paul Smith' in one field.
The query given seems to work on the surname field but not the forename. I knot for certain that the forename columns has these types of data but it is returning no results.
Con
Oracle:
SELECT MyField
from MyTable
where REGEXP_INSTR (MyField, ' ', 1, 2, 0, 'i') > 0
SQL server:
SELECT MyField
from MyTable
where CHARINDEX(' ', MyField, charindex(' ',MyField)+1) > 0
MySQL
select MyField
from MyTable
where length(SUBSTRING_INDEX(MyField, ' ', 2)) < length(MyField)
Here are two solutions that in my opinion are easier to read/understand than JohnHC's.
It can't get any simpler. Use wildcards to search for (at least) two spaces.
SELECT * FROM your_table WHERE your_column LIKE '% % %';
Check the length after replacing the spaces
SELECT * FROM your_table WHERE LEN(your_column) - LEN(REPLACE(your_column, ' ', '')) >= 2;

How to get middle portion from Sql server table data?

I am trying to get First name from employee table, in employee table full_name is like this: Dow, Mike P.
I tried with to get first name using below syntax but it comes with Middle initial - how to remove middle initial from first name if any. because not all name contain middle initial value.
-- query--
select Employee_First_Name as full_name,
SUBSTRING(
Employee_First_Name,
CHARINDEX(',', Employee_First_Name) + 1,
len(Employee_First_Name)) AS FirstName
---> remove middle initial from right side from employee
-- result
Full_name Firstname Dow,Mike P. Mike P.
--few example for Full_name data---
smith,joe j. --->joe (need result as)
smith,alan ---->alan (need result as)
Instead of specifying the len you need to use charindex again, but specify that you want the second occurrence of a space.
select Employee_First_Name as full_name,
SUBSTRING(
Employee_First_Name,
CHARINDEX(',', Employee_First_Name) + 1,
CHARINDEX(' ', Employee_First_Name, 2)) AS FirstName
One thing to note, the second charindex can return 0 if there is no second occurence. In that case, you would want to use something like the following:
select Employee_First_Name as full_name,
SUBSTRING(
Employee_First_Name,
CHARINDEX(',', Employee_First_Name) + 1,
IIF(CHARINDEX(' ', Employee_First_Name, 2) = 0, Len(Employee_First_name), CHARINDEX(' ', Employee_First_Name, 2))) AS FirstName
This removes the portion before the comma.. then uses that string and removes everything after space.
WITH cte AS (
SELECT *
FROM (VALUES('smith,joe j.'),('smith,alan'),('joe smith')) t(fullname)
)
SELECT
SUBSTRING(
LTRIM(SUBSTRING(fullname,CHARINDEX(',',fullname) + 1,LEN(fullname))),
0,
COALESCE(NULLIF(CHARINDEX(' ',LTRIM(SUBSTRING(fullname,CHARINDEX(',',fullname) + 1,LEN(fullname)))),0),LEN(fullname)))
FROM cte
output
------
joe
alan
joe
To be honest, this is most easily expressed using multiple levels of logic. One way is using outer apply:
select ttt.firstname
from t outer apply
(select substring(t.full_name, charindex(', ', t.full_name) + 2, len(t.full_name) as firstmi
) tt outer apply
(select (case when tt.firstmi like '% %'
then left(tt.firstmi, charindex(' ', tt.firstmi)
else tt.firstmi
end) as firstname
) as ttt
If you want to put this all in one complicated statement, I would suggest a computed column:
alter table t
add firstname as (stuff((case when full_name like '%, % %.',
then left(full_name,
charindex(' ', full_name, charindex(', ', full_name) + 2)
)
else full_name
end),
1,
charindex(', ', full_name) + 2,
'')
If format of this full_name field is the same for all rows, you may utilize power of SQL FTS word breaker for this task:
SELECT N'Dow, Mike P.' AS full_name INTO #t
SELECT display_term FROM #t
CROSS APPLY sys.dm_fts_parser(N'"' + full_name + N'"', 1033, NULL, 1) p
WHERE occurrence = 2
DROP TABLE #t

Data is "lastName, FirstName". I need to write it as "FirstName LastName"

Running sql server 2008 The title says it, but in my select statement I have this
COALESCE( ca.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)')
,ca2.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)'))
AS IR_Name
and it returns lastName, FirstName
This becomes a problem when exporting to a csv as it creates two separate columns and what not.
I somehow have to figure out how to have the string be firstName lastName, no commas, and putting the firstName first.
SELECT PARSENAME(REPLACE('John , Doe', ',', '.'), 1) + ' ' + PARSENAME(REPLACE('John , Doe', ',', '.'), 2)
this will switch 'John , Doe' to 'Doe John'. Now, you just need replace the 'John, Doe" with the
COALESCE(ca.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)'),ca2.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)')) AS IR_Name
Another way to do this is by using string functions:
select (case when IR_Name like '%,%'
then (ltrim(rtrim(substring(IR_NAME, charindex(',', IR_NAME)+1, 1000))) + ' ' +
ltrim(rtrim(left(IR_Name, charindex(',', IR_NAME) - 1)))
)
else IR_NAME
end) t
from (select COALESCE( ca.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)')
,ca2.AttributeList.value('(/AttributeList/IRName)[1]','varchar(max)'))
AS IR_Name
. . .
) t
The trimming functions are to remove extra spaces.
The parsename solution is clever. But, it looks strange since parsename is designed for multipart naming conventions, with up to four parts separated by periods.