I'm trying to use 'case when' and 'concat' in one query but it's not working.
I want to get rid of all 'Unidentified', 'N/A' or etc. in last_name or first_name and get a normal full_name but I still get Jane Unidentified or Jane N/A in the output.
Please help!
select
case when (lower(c.last_name) not like '%unidentified%'
and lower(c.last_name) not like '%undefined%'
and lower(c.last_name) not like '%n/%'
and lower(c.last_name) not like '%n?%'
and lower(c.last_name) not like '%general%')
or
(lower(c.first_name) not like '%unidentified%'
and lower(c.first_name) not like '%undefined%'
and lower(c.first_name) not like '%n/%'
and lower(c.first_name) not like '%n?%'
and lower(c.first_name) not like '%general%')
then concat (c.first_name, ' ', c.last_name)
else ' '
end as full_name,
c.first_name,
c.last_name
from `contacts`
How to get rid of the garbage values in the first_name and last_name?
Related
I would like to extract rows that has 'venture' in the Name Column as shown below.
The following SQL code is used to get that result
CASE
WHEN summary.cust_analysis.Name LIKE '%VENTURE%'
However, how can I extract only the first row which has 'Venture' as a word instead of having it as a part of a word like Bonaventure?
If I remove the '%' from the SQL code non of the rows will get extracted.
Appreciate all your help. Thank you :)
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
or
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
OR summary.cust_analysis.Name LIKE '% VENTURE'
OR summary.cust_analysis.Name LIKE 'VENTURE %'
OR summary.cust_analysis.Name = 'VENTURE'
with due concern for upper/lower case presumably too
If you are using SQL Server you can use Regular Expressions.
So you can match entire word followed or preceded by another symbols like dot or comma:
CASE WHEN Name LIKE '%[^A-Z]Venture[^A-Z]%'
OR Name LIKE 'Venture[^A-Z]%'
OR Name LIKE '%[^A-Z]Venture'
OR Name = 'Venture'
This will match ,Venture, Venture., Venture:
More info here
The simplest method is to prepend and postpend the string with spaces:
where concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
You can use the same logic in a CASE expression:
select (case when concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
then 'VENTURE'
end)
Note: This uses the CONCAT() function for string concatenation. The SQL Standard operator is || and some databases have other methods.
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.
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
I have a stored procedure that I am passing in a general string variable called #SearchText as a varchar. This variable contains names, either part of a name, or a full name. I need to do a select on a table based on this variable, using wildcards. The inbound variable could be anything like (for the name 'john smith'):
'j', 'joh', 'john', 'sm', 'smith', 'john s', john smith'... you get the point.
So, the blunt approach I took is
select x from TableA
where FirstName like '%' + #SearchText + '%'
OR LastName like '%' + #SearchText + '%'
Obviously when a space is encountered it screws up the result set. Can someone please help me understand how to tweak this so it can match on any "amount" of the full name?
If this has already been answered, I couldn't find it... a hotlink to an existing solution would be just as appreciated here.
I might suggest something like this:
where FirstName + ' ' + Lastname like '%' + replace(#Searchtest, ' ', '%') + '%' or
LastName + ' ' + Firstname like '%' + replace(#Searchtest, ' ', '%') + '%'
However, if you are trying to do such full text searches, you might consider using a full text index. That generally provides the right level of functionality for these types of queries.
You can do this as follows:
select x from TableA
where FirstName+' '+LastName like '%' + #SearchText + '%'
Basically, you concatenate the first and last name first and apply the LIKE operator on the concatenation.
I have this query:
SELECT *
FROM
(SELECT ' ' + REPLACE(Title,' ',' ') + ' ' AS Title
FROM MyTable) t
WHERE Title LIKE '% Samsung %'
AND Title LIKE '% Galaxy %'
AND Title LIKE '% Axiom %'
AND REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''), ' Axiom ','') = ''
This query should search in MyTable field Title and dispay all rows which contain the words specified in LIKE.
I don't get any error, but the Field Title contains a row with the following string 'Samsung Galaxy Axiom R830' and my query does not return it (and it should).
This was my original question, it worked for some records, but not for all SQL SELECT LIKE containing only specific words
Could it be because you are looking for "Samsung" with a space before & after and the string does not have a space before "Samsung" ?
You have spaces in the like. Perhaps you want something like:
WHERE (Title LIKE '% Samsung %' or title like '%Samsung' or title like 'Samsung%')
AND (Title LIKE '% Galaxy %' or title like '%Galazy' or title like 'Galaxy%')
AND (Title LIKE '% Axiom %' or title like '%Axiom' or title like 'Axiom%')
AND replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
Actually, as I think about it, I think the final replace is sufficient:
where replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
If the title is "Samsung Galaxy Axiom R830", then the following condition will not be true.
REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''),' Axiom ','') = ''
The replaces as written will output
SamsungAxiom R830
This will not match a blank string.
If you removed the spaces from your replaces, you'll be left with R830 (and possibly some whitespace). As Hellion says in his comment, this is a query that requires the words 'Samsung', 'Galaxy' and 'Axiom' to be the only words in your title.