1

I have collected a string from front end for example

string1 = "java, .net, oracle". 

Now I want to compare this string with one which I have in database example

string2 = "sql, oracle, wcf". 

Now I want to compare these strings in simplest way in SQL Server so that when comparing string1 with string2 then as "oracle" is common between both so it will return true.

I know there is a way to split String1 based on the delimiter , and then loop it and compare one by one. But it's seems a lengthy way. Is there any function in SQL Server which can do this?

2
  • 4
    A database should be just a database. It should store data in its most-atomic form, and you should write simple queries to interact with that data. Leave the more-complicated manipulation up to the application layer (i.e. your front-end). You should not be storing multiple pieces of data in one column. If you had a single column called "platform" for example, you could store separate values in separate rows, "java", ".net", "oracle". Then it would be trivial to do what you're trying to do. Your question reveals a really bad database design that you need to fix.
    – rory.ap
    Jun 25, 2016 at 15:19
  • Possible duplicate of TSQL matching as many comma-separated tags as possible
    – paparazzo
    Jun 25, 2016 at 16:18

4 Answers 4

1

You might try this with two derived tables you combine with an INNER JOIN:

DECLARE @s1 NVARCHAR(100)='java,.net,oracle';
DECLARE @s2 NVARCHAR(100)='some1,.net,java';

WITH s1Splitted AS
(
    SELECT LTRIM(RTRIM(A.B.value('.','nvarchar(max)'))) AS Part
    FROM
    (SELECT CAST('<x>' + REPLACE(@s1,',','</x><x>') + '</x>' AS XML) AS y) AS x
    CROSS APPLY x.y.nodes('/x') AS A(B)
)
,s2Splitted AS
(
    SELECT LTRIM(RTRIM(A.B.value('.','nvarchar(max)'))) AS Part
    FROM
    (SELECT CAST('<x>' + REPLACE(@s2,',','</x><x>') + '</x>' AS XML) AS y) AS x
    CROSS APPLY x.y.nodes('/x') AS A(B)
)

SELECT * 
FROM s1Splitted AS s1 
INNER JOIN s2Splitted AS s2 ON s1.Part=s2.Part

This can easily be packed into an inline table valued function which you call as easy as

SELECT * FROM dbo.MyFunction(@s1,@s2);

Attention

If you are expecting input containing the three characters of evil you should replace <, > and & with &lt; &gt; and &amp;, otherwise the XML-split approach would break...

EDIT

As you are looking for the existance of any equality only, a simple COUNT on the result would suffice. You might declare your function as BIT and do this evaluation on function's level, or return the result-set (which would be my approach) and do the evaluation outside...

0

The comment from @roryap is exactly what you should do with your DB. If you cannot perform this then you can create a TVF or SP. That will take string as parameter, split it as a table like

string_to_table
java
.net
oracle

Then using xml or whatever split function, make your table to look like this:

SomeID  Word
1       oracle
1       SQL
1       wcf
2       java
2       .net
2       pho

And then join them to obtain what you need.

0
Declare @InputString Varchar(50) = 'java, .net, oracle'  --<-- String comming in

Declare @Table TABLE (Strings Varchar(50))               --<-- String in the Database
Insert Into @Table Values ('sql, oracle, wcf')

-- Convert to XML Input sting
declare @xml xml = N'<root><r>' + replace(@InputString, ',','</r><r>') + '</r></root>';



WITH DBString AS (

-- Split string stored in the database

SELECT  RTRIM(LTRIM(Split.a.value('.', 'VARCHAR(100)'))) Strings 
FROM   
    (SELECT Cast ('<X>' + Replace(Strings, ',', '</X><X>') + '</X>' AS XML) AS Data
    FROM    @Table
    ) AS t CROSS APPLY Data.nodes ('/X') AS Split(a) )
,InputStrings AS
  (

-- Split String coming in the parameter

    select RTRIM(LTRIM(r.value('.','varchar(max)'))) as InputString
    from @xml.nodes('//root/r') as records(r)
  )

-- Finally Compare the splitted  strings word by word

SELECT * , 'True' [Exist]
FROM InputStrings
WHERE EXISTS (SELECT 1 
              FROM DBString
              WHERE InputStrings.InputString = DBString.Strings)
0

If you are open to some dynamic SQL

What I like about this is that it returns the number of HITS in descending order

Declare @SearchStr varchar(max) ='Cappelletti , Production ,John'
Declare @TableName varchar(150) ='OD'
Declare @FieldName varchar(150) ='[OD-Title]'
Declare @Select    varchar(150) ='[OD-Nr],[OD-Title]'

Set @SearchStr = 'Sign(CharIndex('''+Replace(Replace(Replace(@SearchStr,' , ',','),', ',''),',',''','+@FieldName+'))+Sign(CharIndex(''')+''','+@FieldName+'))'
Declare @SQL nvarchar(Max) = 'Select * from (Select '+@Select+',Hits='+@SearchStr+' From '+@TableName + ') A Where Hits>0 Order by Hits Desc' 
Exec(@SQL)

Returns

OD-Nr   OD-Title                    Hits
2       Cappelletti, John           2
3       Daily Production Summary    1
2
  • Instead of comparing strings now i have created different columns for different entries and that is easier and logical to handle. But now I am getting another error: Msg 4145, Level 15, State 1, Procedure proc_InsertUpdateProfile, Line 69 An expression of non-boolean type specified in a context where a condition is expected, near ','. My query is Jun 26, 2016 at 7:09
  • I'm sorry, why are you asking me? You selected another answer Jun 26, 2016 at 9:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.