Remove the amidst value of a name into other column - sql

I've been working on splitting out some names into several columns in MS SQL server, and what i've got left is a last name and an amidst value (John OF THE Smithsons)
So heres what I want to do, I've got several examples:
of Holland
Of the Clothson,
England, from
So I've got 3 variations of this, (of, of the, from) which can be in random places in the string. What I want to do is remove these values from the LastName column and move them to the Amidst column so I'm left with,
LastName|Amidst
--------------
Holland |of
clothson|of the
England |from
what would be the best way to do this? Is it possible to move the values I want to select into a table and reference from there? I'm not sure if this is possible.

From your description, giant case expressions might be the best way:
select (case when col like '% of the%'
then ltrim(rtrim(replace(col, ' of the', '')))
when col like '% of%'
then ltrim(rtrim(replace(col, ' of', '')))
when col like '% from%'
then ltrim(rtrim(replace(col, ' from', '')))
else col
end) as new_col,
(case when col like '% of the%'
then 'of the'
when col like '% of%'
then 'of'
when col like '% from%'
then 'from'
end) as new_midst

Related

RIGHT of CHARINDEX not selecting correctly

I am trying to parse out a last name field that may have two last names that are separated by either a blank space ' ' or a hyphen '-' or it may only have one name.
Here is what I'm using to do that:
select top 1000
BENE_FIRST_NAME,
BENE_LAST_NAME,
FirstNm =
case
when BENE_FIRST_NAME like '% %' then
left(BENE_FIRST_NAME, CHARINDEX(' ', BENE_FIRST_NAME))
when BENE_FIRST_NAME like '%-%' then
left(BENE_FIRST_NAME, CHARINDEX('-', BENE_FIRST_NAME))
else BENE_FIRST_NAME
end,
LastNm =
case
when BENE_LAST_NAME like '% %' then
right(BENE_LAST_NAME, CHARINDEX(' ', BENE_LAST_NAME))
when BENE_LAST_NAME like '%-%' then
right(BENE_LAST_NAME, CHARINDEX('-', BENE_LAST_NAME))
else BENE_LAST_NAME
end,
CharIndxDash = CHARINDEX('-', BENE_LAST_NAME),
CharIndxSpace = CHARINDEX(' ', BENE_LAST_NAME)
from xMIUR_Elig_Raw_v3
Here are some results:
BENE_FIRST_NAME
BENE_LAST_NAME
FirstNm
LastNm
CharIndxDash
CharIndxSpace
JUANA
PEREZ-MARTINEZ
JUANA
RTINEZ
6
0
EMILIANO
PICENO ESPINOZA
EMILIANO
SPINOZA
0
7
JULIAN
NIETO-CARRENO
JULIAN
ARRENO
6
0
EMILY
SALMERON TERRIQUEZ
EMILY
TERRIQUEZ
0
9
The CHARINDEX seems to be selecting the correct position but it is not bringing in all of the CHARs to the right of that position. Sometimes it works like in the last record. But sometimes it is off by 1. And sometimes it is off by 2. Any ideas?
If you need to select part of a last name after space/hyphen, you need to get right part of the string with length = total_lenght - space_position:
...
LastNm =
case
when BENE_LAST_NAME like '% %' then
right(BENE_LAST_NAME, LEN(BENE_LAST_NAME) - CHARINDEX(' ', BENE_LAST_NAME))
when BENE_LAST_NAME like '%-%' then
right(BENE_LAST_NAME, LEN(BENE_LAST_NAME) -CHARINDEX('-', BENE_LAST_NAME))
else BENE_LAST_NAME
end,
...
Your last name logic doesn't make sense..
RIGHT takes N chars from the right of the string
CHARINDEX gives the position of a char from the left of the string
You can't use it to find a position from left and then take that number of chars from the right of the string
Here's a name:
JOHN MALKOVICH
The space is at 5. If you take 5 chars from the right, you get OVICH. The shorter the name before the space and the longer the name after the space, the fewer chars you get from the last name
Perhaps you mean to put a LEN in there so you take the string length minus the index of the space.. You can also use it in a call to SUBSTRING as the start index, and tell SQLS to take 9999 chars (of any number longer than the remaining string) and it will take up to the end of the string
SUBSTRING(name, CHARINDEX(' ', name)+1, 9999)
I think you can simplify your code by a lot. Consider below with a different but representative sample data
with data (name) as
(select 'first-last' union select 'first last' union select 'firstlast'),
data_prepped (name, indx) as
(select name,coalesce(nullif(charindex(' ', name)+charindex('-', name),0),len(name))
from data)
select name,
left(name, indx-1) as part1,
right(name, indx) as part2
from data_prepped

How to return 0 with where condition user when user search only space character?

Hello I'm using Oracle 11g and i have a data that looks like this
no|name|flag
------------
1|kumar|1
2|rajesh singh|1
3|adi sneedar|1
4|danielle castro|1
5|cef danish|1
if i did
select count(*) from tablename where name like '% %'
it will return 2 records.
if i did "multiple spaces", like 2 or more spaces
select count(*) from tablename where name like '% %'
it returns 0(this means good)
it will return 5 records.
What i want is if the user only input '% %' it will also return 0. But i also wanted that
select count(*) from tablename where name like '%adi sneedar%'
it will return 1
How should i do that in the where condition?
Something like this might suffice, assuming that the '%' are being passed as part of the binding input
select *
from mytable
where name like :bindvar
and replace(replace(:bindvar,'%'),' ') is not null
which basically says they need to enter something that is not solely spaces and percentage signs.

Any way to change specific data in a column e.g Mr to Dr which contains a name

I am trying to change the Titles of 'doctors' in a database and was just wondering was there a SQL query which I could run to change them.
The column im trying to change
What I am asking is that there is any way I can update the column to add a 'Dr' infront of the names to replace the 'Miss','Mr' etc.
I'm thinking about using a SQL Statement containing the wildcard function to update it but not sure it would change the specifics.
Thanks,
Karl
Use REPLACE option
UPDATE table_name
SET column_name = REPLACE(column_name, 'Mr.', 'Dr.')
WHERE column_name LIKE 'Mr.%'
try this
Update myTable
set name = replace(replace(name,'Miss ','Dr '),'Mr ','Dr ')
I might suggest doing:
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr');
This only replaces the first word in the string. You might want to be extra careful and add a where clause.
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr')
where col like 'Miss %' or col like 'Mr %' or
col like 'Mrs %' or col like 'Ms %';
The problem with replace() is that it replaces all occurrences in the string. Although the honorifics are unlikely to be in a name, you could have names like "Missy Elliott".

Select only when the field has more than one word

SELECT name FROM clients;
Table clients
id | name |
1 John
2 John Bravo
3 John Alves
4 Jo
In postgres, how can I select only names with more than one word? For example. This should be the output:
John Bravo
John ALves
I think just test if the name contains a space: where name like '% %'
But this will give you some problem if you name can contain space char befor or after the name, like: ' JOHN' or 'Luck '
If word means to you a space delimited token, then the following would do the trick:
SELECT name FROM clients WHERE name LIKE '% %';
But this will also give you those that have empty names made out of spaces only. Also performance-wise, this will be a costly query.
First you may have to find the number of words in the column, then only select where the number of words is greater than 1.
For example:
SELECT LENGTH(name) - LENGTH(REPLACE(name , ' ', ''))+1 FROM clients;
This will return the number of words in each row, which we have in the field name.
Then you can proceed putting this in a nested select SQL statement something like :
SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 as mycheck, name FROM clients where (SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)>1
This will return only where words are greater than 1 (>1). Else you can user >2 to return columns with more than 2 words.
LIKE is an option, or you can use split_part:
SELECT name FROM clients WHERE split_part(trim(name), ' ', 2) <> ''

SQL Search column for each variable in CSV string

I have a variable passed to stored procedure
Ex:
#keywords = 'val1, val3, val5'
And i'm trying to see if column named Title contain any of them in it
Ex: Title1 - 'Hello val1'
Title2 - 'Hello val3'
Title3 - 'Hello val1, val3'
Title4 - 'Hello'
SO my results should return values
Title
------
Hello val1
Hello val3
Hello val1, val3
Is this possible to use LIKE or any other function/method?
You need to split the CSV into rows (see Arrays and Lists in SQL Server 2005 and Beyond for variuos techniques how). I'll assume that you create dbo.ufnSplitRows based on this
Then JOIN using LIKE
SELECT *
FROM
MYtable M
JOIN
dbo.ufnSplitRows (#CSV) C ON M.Title LIKE '%' + C.SplitValue + '%'
By the way, it will run poorly because of the leading '%' at least
If you make some assumptions about how the query string is stored, then yes (though it's not terribly efficient):
Assumption: the string will be stored with every item separated by a comma, then a space. (This is what you posted in your question)
select * from YourTable where
(#keywords like KeyColumn + ', %') or
(#keywords like '%, ' + KeyColumn + ', %') or
(#keywords like '%, ' + KeyColumn)