How can I compare two columns for similarity in SQL Server? - sql

I have one column that called 'message' and includes several data such as fund_no, detail, keywords. This column is in table called 'trackemails'.
I have another table, called 'sendemails' that has a column called 'Fund_no'.
I want to retrieve all data from 'trackemail' table that the column 'message' contains characters same as 'Fund_no' in 'trackemails' Table.
I think If I want to check the equality, I would write this code:
select
case when t.message=ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
But, I do want something like below code:
select
case when t.message LIKE ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
I would be appreciate any advice to how to do this:

SELECT *
FROM trackemails tr
INNER JOIN sendemail se on tr.Message like '%' + se.Fund_No + '%'

Dear Check SQL CHARINDEX() Function. This function finds a string in another string and returns int for the position they match. Like
SELECT CHARINDEX('ha','Elham')
-- Returns: 3
And as you need:
SELECT *
,(SELECT *
FROM sendemail
WHERE CHARINDEX(trackemails.Message,sendemail.Fund_No)>0 )
FROM trackemails
For more information, If you want something much better for greater purposes, you can use Fuzzy Lookup Component in SSDT SSIS. This Component gives you a new column in the output which shows the Percentages of similarity of two values in two columns.

Related

Need to find string using bigquery

We have below string column and having below data
and I want to find Null count present in string columns means how many times null value('') present in front of id column present in select statement
using big query.
Don't use string position.
Expected output:
count of null ('')id =3
1st row,2nd row and 5th row
Below is for BigQuery Standard SQL
#standardSQL
SELECT
FORMAT(
"count of null ('')id = %d. List of id is: %s",
COUNT(*),
STRING_AGG(CAST(ID AS STRING))
) AS output
FROM `project.dataset.table`
WHERE REGEXP_CONTAINS(String, r"(?i)''\s+(?:as|)\s+(?:id|\[id\])")
if to apply to sample data from your question - the output is
Row output
1 count of null ('')id = 3. List of id is: 1,2,5
The idea is to unify all strings to something you can query with like = "%''asid%" or regex
First replace all spaces with ''
replace "[", "]" with ''.
Make the use of " or ' consistent.
Then query with like.
For example:
select 1 from (select replace(replace(replace(replace('select "" as do, "" as [id] form table1',' ',''),'[',''),']',''),'"',"'") as tt)
where tt like ("%''asid%")
Its not a "smart" idea but its simple.
A better idea will be to save the query columns in a repeat column '"" as id' and the table in another column.
You don't need to save 'select' and 'from' this way you can query easily and also assemble a query from the data.
If I understand correctly, you want to count the number of appearances of '' in the string column.
If so, you can use regexp_extract_all():
select t.*,
(select count(*)
from unnest(regexp_extract_all(t.string, "''")) u
) as empty_string_count
from t;

SQL full text search behavior on numeric values

I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other

MS Access Expression to Find Records Where the Value to the Left of a Comma is Greater than Zero

I have some records in a column that contain IDs and some of these records contain multiple IDs separated by commas. Additionally there are some records where I have ",3" and ",2" when they should simply be "3" and "2". I do not have write privileges in this DB so updating those records is not an option.
I am trying to write a query that returns records that have a comma where the value to the left of any comma in the record is greater than 0 e.g. "2,3", "2,3,12" etc but NOT ",3" or ",2".
What would this expression look like in MS Access?
Thanks in advance.
If you want to remove the starting comma from the records when you return them, you can do so using a simple query:
SELECT IIF(MyField LIKE ",*", Right(MyField, Len(MyField)-1), MyField)
FROM MyTable
To answer your original question, you could simply use Val:
SELECT * FROM YourTable WHERE Val([YourField]) > 0
I would simply use:
select t.*
from t
where val not like ",*";
This doesn't handle the 0 part, but you don't give any examples in your answer. Perhaps this answers that part:
select t.*
from t
where val not like ",*" and val not like "*0,*";

Need to check parameter against table rows in SQL Query

I would like to build one sql query in that one of my filed of form should not contain common names (maintained list of words in separate table) and i am passing value of that filed as parameter and want to check that it shouldn't contain any common name from that table.
How can i achieve that using sql query?
Note : if common name is 'abc' and i am passing parameter as '!abc123' since it contains that word query should return false.
Thanks in advance.
Try something like (Untested Query):
SELECT CommonName
FROM CommonNamesTable
WHERE CommonName like '%NameToTest%'
OR CONTAINS(NameToTest, CommonName);
Basically you need the string match options:
Take a look at options of CONTAINS and read about Queries with full text search
Is this what you're looking for?
SELECT (COUNT(*) == 0) FROM tablewithcommonwords
WHERE wordfromform LIKE CONCAT('%', wordcolumnnfromcommonwordstable, '%');
Try this:
IF NOT EXISTS(SELECT word FROM CommonWord WHERE #yourparam
LIKE '%' + word + '%')
BEGIN
RETURN 1
END
ELSE
BEGIN
Return 0
END
This works if the #yourParam is contained in any word or name, what you do not want to use. It only returns 1 if it is not contained by any row in the table.
I worte this sentence only on this way (you can use a simple Exists instead of NOT Exists), because may you want to extend the functionality in the true part.
if exists (select * from reservedwords where #parameter like '%'+word + '%')
select 0
else
select 1
I would like to suggest that You have to use keypress Event in Your TextBox and then Handle your Code after Each character enter in your TextBox.

Is it possible to use LIKE and IN for a WHERE statment?

I have a list of place names and would like to match them to records in a sql database the problem is the properties have reference numbers after there name. eg. 'Ballymena P-4sdf5g'
Is it possible to use IN and LIKE to match records
WHERE dbo.[Places].[Name] IN LIKE('Ballymena%','Banger%')
No, but you can use OR instead:
WHERE (dbo.[Places].[Name] LIKE 'Ballymena%' OR
dbo.[Places].[Name] LIKE 'Banger%')
It's a common misconception that for the construct
b IN (x, y, z)
that (x, y, z) represents a set. It does not.
Rather, it is merely syntactic sugar for
(b = x OR b = y OR b = z)
SQL has but one data structure: the table. If you want to query search text values as a set then put them into a table. Then you can JOIN your search text table to your Places table using LIKE in the JOIN condition e.g.
WITH Places (Name)
AS
(
SELECT Name
FROM (
VALUES ('Ballymeade Country Club'),
('Ballymena Candles'),
('Bangers & Mash Cafe'),
('Bangebis')
) AS Places (Name)
),
SearchText (search_text)
AS
(
SELECT search_text
FROM (
VALUES ('Ballymena'),
('Banger')
) AS SearchText (search_text)
)
SELECT *
FROM Places AS P1
LEFT OUTER JOIN SearchText AS S1
ON P1.Name LIKE S1.search_text + '%';
well a simple solution would be using regular expression not sure how it's done in sql but probably something similiar to this
WHERE dbo.[Places].[Name] SIMILAR TO '(Banger|Ballymena)';
or
WHERE dbo.[Places].[Name] REGEXP_LIKE(dbo.[Places].[Name],'(Banger|Ballymena)');
one of them should atleast work
you could use OR
WHERE
dbo.[Places].[Name] LIKE 'Ballymena%'
OR dbo.[Places].[Name] LIKE 'Banger%'
or split the string at the space, if the places.name is always in the same format.
WHERE SUBSTRING(dbo.[Places].[Name], 1, CHARINDEX(dbo.[Places].[Name], ' '))
IN ('Ballymena', 'Banger')
This might decrease performance, because the database may be able to use indexes with like (if the wildcard is at the end you have even a better chance) but most probably not when using substring.