Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Select similar text

Tags:

sql

sql-server

I have a table Team

Id  Name ...
1    Chelsea
2    Arsenal
3    Liverpool 

Now I need to search if my team table has a name like "Chelsea FC". How could I make a select query in this case when search string may have extra words ?

I could try Lucene.net but it's kind of overkill just for a small usage and it would take time to learn it.

like image 799
nam vo Avatar asked May 02 '15 05:05

nam vo


People also ask

How do I find similar text in SQL?

Using the Levenshtein distance method This method can be used among others (Soundex, LIKE statement, Regexp) to perform string similarity or string matching in order to identify two elements (text, strings, inputs) that are similar but not identical.

How do you select similar data in SQL?

To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.

How do I find similar names in SQL?

To find the duplicate Names in the table, we have to follow these steps: Defining the criteria: At first, you need to define the criteria for finding the duplicate Names. You might want to search in a single column or more than that. Write the query: Then simply write the query to find the duplicate Names.


2 Answers

You can use like this way:

declare @s varchar(20) = 'Chelsey FC'

select * from Team
where name like '%' + @s + '%' or
        @s like '%' + name + '%'

This will filter rows if @s contains Name or Name contains @s.

like image 59
Giorgi Nakeuri Avatar answered Oct 17 '22 22:10

Giorgi Nakeuri


You would need to split the string up and search by each word in the string. SQL Server doesn't have a native function to do that, but there are various examples on the web.

This function will take a string and a delimiter, and it will split the string by the delimiter and return a table of the resulting values.

CREATE FUNCTION dbo.SplitVarchar (@stringToSplit varchar(4000), @delimiter CHAR(1))
RETURNS  @Result TABLE(Value VARCHAR(50))AS
BEGIN
    --This CTE will return a table of (INT, INT) that signify the startIndex and stopIndex
    --of each string between delimiters.
    WITH SplitCTE(startIndex, stopIndex) AS
    (
      SELECT 1, CHARINDEX(@delimiter, @stringToSplit)   --The bounds of the first word
      UNION ALL
      SELECT stopIndex + 1, CHARINDEX(@delimiter, @stringToSplit, stopIndex+1)
      FROM SplitCTE             --Recursively call SplitCTE, getting each successive value
      WHERE stopIndex > 0
    )

    INSERT INTO @Result
    SELECT
        SUBSTRING(@stringToSplit,   --String with the delimited data
            startIndex,             --startIndex of a particular word in the string
            CASE WHEN stopIndex > 0 THEN stopIndex-startIndex   --Length of the word
            ELSE 4000 END   --Just in case the delimiter was missing from the string
            ) AS stringValue
    FROM SplitCTE

    RETURN
END;

Once you turn your delimited string into a table, you can JOIN it with the table you wish to search and compare values that way.

DECLARE @TeamName VARCHAR(50)= 'Chelsea FC'

SELECT DISTINCT Name
FROM Team
INNER JOIN (SELECT Value FROM dbo.SplitVarchar(@TeamName, ' ')) t
  ON CHARINDEX(t.Value, Name) > 0

Results:

|    Name |
|---------|
| Chelsea |

SQL Fiddle example

I based my design on Amit Jethva's Convert Comma Separated String to Table : 4 different approaches

like image 41
Phil Walton Avatar answered Oct 17 '22 22:10

Phil Walton