0

I have a table with a column and value JobSkill = ".net sap lead". Now user enter the value "abap sap hana". I want to include a where condition which match exactly 3 or more continuous characters including space. In above scenario both have common "sap" substring so the condition should result in true. Below is my query. Please help. Previously I am using charindex but it does not resolve the purpose. I am using sql server 2008

SELECT Email_Id, JobSkill FROM Jobs
WHERE CHARINDEX(JobSkill, "abap sap hana") > 0
3
  • 1
    The Full Text index will work best in your case Commented Aug 17, 2016 at 5:33
  • It would be better to store the skills as discrete values, rather than stuffing them all into a string and then struggling to write meaningful queries against them because string isn't a data type designed for storing multiple values. Commented Aug 17, 2016 at 7:06
  • i gone through Full Text article but I think it will work like "LIKE" statement only thing is it is efficient. Can you tell me how it will work in my case. Commented Aug 17, 2016 at 7:11

2 Answers 2

1

You need to create a function which loops through all positions of characters of String1 except the last 2, and check if String2 is like '%' + [(x,x+1,x+2)] + '%' string, where x is current position.

So for stings ('abcd acd g', 'ert acd'),

it should check

'ert acd' like '%abc%'
'ert acd' like '%bcd%'
'ert acd' like '%cd %'
'ert acd' like '%d a%'

and so on...

If like returns TRUE, break the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

i am working on creating this function but I doubt whether it will be a efficient technique because if there are 15 character in a string then it have to compare 12 substrings. Is there any method for substring match in sqlserver2008
If performance is a concern, CLR functions (which implement similar logic)are faster, but you need to have programming skills in other languages, like C#. I have also another idea but not sure if it would be faster. You can create pre-calculated table like (3,1,3),(4,1,3),(4,2,4),(5,1,3),(5,2,4),(5,3,5),(6.... first column is length, second is start position, third is end position. Then join with that table by ON Lengh(<string>) = Table.Length and then use SUBSTRING + LIKE (OR CHARINDEX) to compare. The comment is a not good place to describe it.
IF the length is fixed to 15, then that table should have only 12 rows
0

Try like this,

SELECT j.Email_Id
    ,j.JobSkill
FROM Jobs j
INNER JOIN (
    SELECT LTRIM(RTRIM(m.n.value('.[1]', 'varchar(8000)'))) SearchString
    FROM (
        SELECT CAST('<XMLRoot><RowData>' + REPLACE(@Input, ' ', '</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
        ) t
    CROSS APPLY x.nodes('/XMLRoot/RowData') m(n)
    ) T ON j.JobSkill LIKE '%' + T.SearchString + '%'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.