how to select record whose matching percentage is higher than other using like operator in sql server?

后端 未结 3 926
温柔的废话
温柔的废话 2021-01-14 23:45

I have set of records which I need to search using criteria. But criteria is returning me multiple rows.

So I need top 2 records which are having maximum percentage

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 00:34

    Given solutions not worked for me,

    So I created my own logic:

    SELECT TOP 2 FirstName, LastName, CountryName, StateName 
    FROM Employee
    WHERE
        statename like '%Gujarat%' AND countryname like '%India%'
    ORDER BY
        LEN(StateName + CountryName) - LEN(REPLACE(StateName, 'Gujarat', '') + REPLACE(CountryName, 'India', '')) DESC
    

    Hope this help...

提交回复
热议问题