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.
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
So this is the data I am pulling in from powershell (there's actually more, but it all follows the same pattern):
Name : SULESRKMA 1
Location : Leisure Services - Technology Services 2
DriverName : KONICA MINOLTA mc4695MF PS PPD 3
Shared : False 4
ShareName : 5
JobCountSinceLastReset : 0 6
I am trying to remove 'Name : ' and 'Location : ', and so on, but using the REPLACE command here is part of my sql query:
SELECT * FROM #ReadCmd
WHERE Result LIKE 'Name %:%'
INSERT INTO #output(Name)
SELECT REPLACE(Result, '% %: %', '')
From #ReadCmd
WHERE Result LIKE 'Name %:%'
SELECT * FROM #output
Or for example, here:
IF OBJECT_ID('tempdb..#fields') != 0
DROP TABLE #fields
CREATE TABLE #fields (Fields varchar(256))
INSERT INTO #fields (Fields)
SELECT REPLACE(Result, ' %: %', '')
FROM #ReadCmd
Where Result Like '% %: %'
The point is, I'd like to replace the '_________ : ' with nothing, but the REPLACE command reads the sql wild card '%' as an actual percent sign. Is there another way to accomplish this?
Using Select RIGHT(Result,CHARINDEX(': ',Result) -1) outputs in seperate cells:
: SULESRKMA
: PrimoPDF
oft XPS Document Writer
: Fax
: CutePDF Writer
you can try
SELECT * FROM #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';
if you want select only the info after the : then you should use
SUBSTRING(Result, CHARINDEX(':',Result) +2, 255)
from #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';
Select RIGHT(ColumnName,CHARINDEX(':',ColumnName) -1)
See SQL string manipulation [Get all text left of '('] for reference.
That is unless I am misunderstanding your question and you want to keep Name and the : in there and just remove the spaces. If that is so your second and fourth non code lines are contradictory.
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.