SQL WHERE CONTAINS statement referencing columns not static text - sql

I have these data below:
FName | Lname | Notes | Flag
----- ----- ----- ----
John Smith John Smith didn't know 1
Bill Jones Tom Johnson knows 0
I need to create and populate a variable (Flag) which looks at the Notes column, searches for values in Fname and Lname column, and assigns a 1 if Notes contains both Fname and Lname values, or 0 if it contains only 1, or none of the values from Fname and Lname.
I have looked at CONTAINS, LIKE, and INSTR, but I am having trouble with the references, the examples I see want me to enter a static value to search on ("John"), rather than a column reference ("Fname").
I'd like to write a CASE statement that says
Look in each row, if the Notes field contains the values in the Fname and Lname fields, Flag =1, else, Flag =0.
I have tried
CASE
WHEN
(
SELECT
Fname, Lname, Notes
FROM
table
WHERE CONTAINS ((Notes, Fname) AND (Notes, Lname)) > 0
)
THEN '1' ELSE '0' END
AS Flag
FROM table;
Any help is appreciated. Thank you !

Not sure if the coalesce/isnull is necessary.
select
case
when ((Notes like '%' + coalesce(u.FName,'') + '%') and (Notes like '%' + coalesce(LName,'') + '%'))
then 1
else 0
end Flag
from Table
update t
set t.Flag =
case
when ((Notes like '%' + coalesce(u.FName,'') + '%') and (Notes like '%' + coalesce(LName,'') + '%'))
then 1
else 0
end
From Table t

You were close:
SELECT
CASE WHEN
t.Notes LIKE '%' + t.FName + '%'
AND
t.Notes LIKE '%' + t.LName + '%'
THEN 1
ELSE 0 END AS Flag
FROM
table AS t
However, if you really just want to filter where the Flag = 1, you don't need this as a column:
SELECT
*
FROM
table AS t
WHERE
CASE WHEN
t.Notes LIKE '%' + t.FName + '%'
AND
t.Notes LIKE '%' + t.LName + '%'
THEN 1
ELSE 0 END =1

Related

How can I check if string value in two columns also exists in another column in SQL

So there are 3 columns, first name, last name, and description.
What I want to do is to check if first name or last name, or both, exist in description.
I believe using Like operator might help, but not sure how to write it.
Any suggestion is appreciated!
You can use a case expression and like. In Standard SQL, this would look like:
select t.*,
(case when description like '%' || firstname || '%' or
description like '%' || lastname || '%'
then 1 else 0
end) as in_description_flag
from t;
This uses the SQL standard operator for string concatenation. Some databases have bespoke operators (such as +) or bespoke functions (such as concat()).
EDIT:
In SQL Server:
select t.*,
(case when description like '%' + firstname + '%' or
description like '%' + lastname + '%'
then 1 else 0
end) as in_description_flag
from t;

How to use Case statement in SQL correctly

The below Select statement does NOT return a record for me:
Select 1:
SELECT
Case When rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join Bil_ManualPaymentDetails bmpd
on rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%' + ''
Output:
System =
The below Select statement DOES return a record for me:
Select 2:
SELECT
Case When rcdr.PolicyNumber like '1234567890%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join BilManualPaymentDetails bmpd
on rcdr.PolicyNumber like '1234567890%'
Output:
System = Adc
Am I missing a tick '' mark somewhere in the first Select ?
BilManualPaymentDetails Table:
PaymentReferenceNumber
123456789020161013025120
RcDetailRecords Table:
PolicyNumber
1234567890
1234567890 is not like 123456789020161013025120%, but 123456789020161013025120 is like 1234567890%
So it appears you have the fields switched, try:
SELECT
CASE WHEN bmpd.PaymentReferenceNumber LIKE '' + rcdr.PolicyNumber + '%'
THEN 'Adc'
ELSE 'Exceed'
END as System
FROM RcDetailRecords rcdr
JOIN Bil_ManualPaymentDetails bmpd
ON bmpd.PaymentReferenceNumber like '' + rcdr.PolicyNumber + '%' + ''

sql order by best matched results from one specific where clause

Hi I'm using sql server 2008 and have a big stored procedure which has multiple where clauses. Within one of those clause I have the following:
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
I'm looking to show results in an order, whereby I get the results where BOTH matched in clause 7 first, followed by if one matched. Is this possible? I've tried a few things but nothing has worked so far. E.g:
SELECT
BLAH BLAH
FROM MULTIPLE TABLE JOINS
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
ORDER BY
(CASE WHEN COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%' THEN 1 ELSE 0 END) DESC
and
SELECT BLAH BLAH, (case when COL1 LIKE '%'+ #VARIABLE + '%' then 1 else 0 end) +
(case when COL2 LIKE '%'+ #VARIABLE + '%' then 1 else 0 end) as [priority]
FROM MULTIPLE TABLE JOINS
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
ORDER BY [Priority]
And a few more but nothing has worked...
Your first query is ordering based on your condition:
CASE WHEN COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%' THEN 1 ELSE 0 END
But it's redundant with your WHERE clause:
COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%'
Add the CASE statement from your ORDER BY to your SELECT and you'll see it's 1 for every row, so the ORDER BY is working as it should, it just has no impact since all records are being ordered by 1, you need to alter the ORDER BY criteria, if you give additional detail about how you want it ordered someone can surely help.

Select using case shows all results

Two fields in the table articles:
title and body
Two records:
meow bark | 1234 5678 and bird snake | 8596 2952
Where meow bark is under title and 1234 5678 is under body, etc...
SQL Query:
SELECT *,
CASE WHEN title = 'meow' then 1 else 0 end + CASE WHEN title = 'bark' then 1 else 0 end AS matches
FROM articles
ORDER BY matches DESC
Returns both rows. Please help me understand what's wrong with my syntax. I am expecting it to return only rows where the title matches what is in the query. Unless I do not understand how CASE works, if title in the above example matches meow or bark, it should display - else, do not display.
The goal is to return results based on most matches. Take at fiddle look here: http://sqlfiddle.com/#!3/13b33/10
I modified it slightly, didn't think enough to break it though.
Unsure what you want to achieve ...what would you like to be returned
This might be a bit closer to what you want
SELECT *,
matches = CASE WHEN title LIKE '%meow5' THEN 1 ELSE 0 END + CASE WHEN title LIKE '%bark5' THEN 1 ELSE 0 END
FROM articles
ORDER BY matches desc
EDIT after OP has been updated
Try using the WHERE clause if you only want some of the records to be returned:
SELECT *
FROM articles
WHERE title LIKE '%meow%'
OR
title LIKE '%bark%'
ORDER BY matches desc
FINAL EDIT
Try this fiddle
In case Fiddle goes out of date here is the script:
CREATE TABLE YourTable (FieldToSearch varchar(max));
INSERT INTO YourTable
VALUES
('If there were a dog'),
('If there was a dog'),
('If that dog died!'),
('I''d be happy with cat');
SELECT x.*
FROM (
SELECT FieldToSearch,
CASE WHEN ' ' + FieldToSearch + ' ' like '% if %' then 1 else 0 end
+ CASE WHEN ' ' + FieldToSearch + ' ' like '% there %' then 1 else 0 end
+ CASE WHEN ' ' + FieldToSearch + ' ' like '% was %' then 1 else 0 end
+ CASE WHEN ' ' + FieldToSearch + ' ' like '% a %' then 1 else 0 end
+ CASE WHEN ' ' + FieldToSearch + ' ' like '% dog %' then 1 else 0 end AS matches
FROM YourTable
)x
WHERE x.matches >0
ORDER BY x.matches DESC;
You need to put that in a WHERE clause rather than the SELECT clause if you want to exclude rows.
Also, the comparisons you are using are for equivalence. This will not "match" if the column contains your search value, as you seem to think, but only if the column has exactly the same text and nothing else (with some small allowances for collation).
you dont have where clause mentioned in the query, it will fetch all the records unless you filter them using where condition

Compare Columns Where One is Similar to Part of Another

I'm trying to write a Select statement where I can see if one column is like part of another.
tblNames
ID FullName FirstName
1 Mr. John Doe, CEO John
2 Mr. Jake Doe, Exec Jake
3 Mrs. Betty Smith, Chair Jill
The query should return:
3 | Mrs.Betty Smith, Chair | Jill
However mine just returns every row in the table:
SELECT ID, FullName, FirstName
FROM tblNames
WHERE '%' + FirstName + '%' not like Fullname
Any ideas?
Reverse the where, to something like this:
Fullname not like '%' + FirstName + '%'
This worked for me:
SELECT *
FROM `table`
WHERE `col1` NOT LIKE CONCAT('%', `col2`, '%')
Found it here:
http://www.edmondscommerce.co.uk/mysql/compare-two-columns-in-mysql/
Somehow it only worked correctly with the concat-function (?).
Try this:
SELECT * FROM tblNames
WHERE ISNULL( CHARINDEX (FirstName , FullName),0) = 0
The CHARINDEX will be faster (more performant) than a LIKE clause, as it doesn't have to take wildcards into account. The sample data above with small numbers of rows won't show a performance benefit, but when in the millions of rows, CHARINDEX would perform better.
Switch the arguments to LIKE in the WHERE clause:
SELECT ID, FullName, FirstName
FROM tblNames
WHERE Fullname not like '%' + FirstName + '%'
The wildcard must be the second argument.
Oracle expects number when + is used. For string, please use the samle below :
SELECT ID, FullName, FirstName
FROM tblNames
WHERE FullName like '%' || FirstName||'%'
To compare one column of a table to a column of another table, please do the following
select a.*,table_2_col_1, table_2_col_2 from (select table_1_col_1, table_1_col_2 from table_1 where
) a, table_2 where table_1_col_1 like '%' || table_2_col_1 ||'%'
It looks OK, except you probably want to switch the order around in your where:
WHERE Fullname not like '%' + FirstName + '%'
Parentheses also would have fixed the issue.
SELECT ID, FullName, FirstName
FROM tblNames
WHERE ('%' + FirstName + '%') not like Fullname
With Google BigQuery:
Fullname NOT LIKE ('%' || FirstName || '%')