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.
Related
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 tried to categorized the comment with keyword in SQL Server but I don't know how to get a specific string.
The word I need is ice only, but with the code '%ice%' I will get 'notice', 'service'...
SELECT
comment,
(CASE WHEN comment LIKE '%ice%' THEN 'Ice' END) AS comment_category
FROM events
Any suggestion on how to solve this?
If comment consists of words separated by spaces, you can do:
' ' + comment + ' ' like '% ice %'
You can even add other delimiters, such as:
' ' + comment + ' ' like '%[- ,.()]ice[- ,.()]%'
You can also use SQL Server's kinda sorta regex:
'.' + comment + '.' like '%[^a-z0-9]ice[^a-z0-9]%'
I need a query to get string with space in between.
i.e it should not return strings like
' abc', 'abc ' and ' abc '
and it should return strings like
'ab c' ,' ab c', 'ab c ' and ' ab c '
i tried with below query.
select user_fname,user_lname
from user where user_fname like '% %';
but it is returning all the rows.
% matches zero or more characters. I'd suggest adding some _s in:
select user_fname,user_lname
from user where user_fname like '%_ _%';
If that's still matching too much, perhaps:
select user_fname,user_lname
from user where user_fname like '%[^ ] [^ ]%';
Which will match zero or more characters, then something that definitely isn't a space, a space, something that definitely isn't a space and then zero or more characters.
Try this:
SELECT user_fname,user_lname
FROM user
WHERE user_fname LIKE '% %' AND user_fname NOT LIKE ' %' AND user_fname NOT LIKE '% '
you could try this also
select user_fname,user_lname
from user lTrim(rTrim(user_fname)) LIKE '% %'
this will remove the white space left and right of user_fname and then it will try to find the white space between the character.
I have a text like following on a database field (SQL Server 2000)
"Led sledding leding led go led"
I want SQL Command to replace the word led to LED, But it shouldn't change words like "sledding" / "leding"
There are 15,000 records with similar text. Need to apply this for all of them.
I have tried following but it takes more than 24 hours. (With in a cursor)
update rprd
set dsc = replace(dsc, 'led ', 'LED ')
where dsc not like 'LED %' collate sql_latin1_general_cp1_cs_as
and dsc like 'led %'
update rprd
set dsc = replace(dsc, ' led ', ' LED ')
where dsc not like '% LED %' collate sql_latin1_general_cp1_cs_as
and dsc like '% led %'
update rprd
set dsc = replace(dsc, ' led', ' LED')
where dsc not like '% LED' collate sql_latin1_general_cp1_cs_as
and dsc like '% led'
Please suggest me a faster and simple way of doing this.
You didn't (edit: didn't initially) specify which database you are using.
Most database vendors have a function with general syntax like, or very close to REPLACE( source-string, from-string, to-string ), but the syntax will vary in terms of what kinds of wildcards you can use or whether you can use regular expressions, and case-sensitivity differs among vendors with regard to object names and string lookups. However, replacing a string with a string of a specific case will work with every vendor.
For your first pass, you might try something as simple as replacing ' led ' (led with a space on either side of it), like this:
REPLACE( somefield, ' led ', ' LED ' )
TSQL does support some modestly advanced wildcard searches: https://msdn.microsoft.com/en-us/library/ms179859.aspx
I take it, you just need to correct some existing data once, and you dont need a solution to use constantly.
so why dont you run a few queries that solve your problem easily and quickly,
UPDATE table SET field=regexp_replace(field, '^led ', 'LED ');
UPDATE table SET field=regexp_replace(field, ' led$', ' LED');
UPDATE table SET field=regexp_replace(field, ' led ', ' LED ');
make sure to check your db docs on correct syntax of your functions
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.