Difference between CONTAINS and LIKE clauses in search | Community
Skip to main content
Level 9
January 5, 2016
Solved

Difference between CONTAINS and LIKE clauses in search

  • January 5, 2016
  • 8 replies
  • 4056 views

Hi All,

CONTAINS(s.[jcr:title],'abc') OR (s.[jcr:title] LIKE 'abc%'))  

The above line is being used in JCR-SQL2 query.

1]What exactly does CONTAINS and LIKE do here.

Any thoughts on this will be helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by GK-007

Please see below.

  • Below Queries will find any node with a title property which contains the text client any where in the text phrase.

            SELECT * FROM [nt:base] AS s WHERE CONTAINS(s.title, 'client')

           SELECT * FROM [nt:base] AS s WHERE s.title like  '%client%'

  •  Query will find files with ends with the string ".png" extension

            SELECT * FROM [nt:file] WHERE NAME() LIKE '%.png'

  • Query will only find the files starts with the name 'abc'

            SELECT * FROM [nt:file] WHERE NAME() LIKE 'abc%'

Hope this clears difference between LIKE and COONTAINS

8 replies

GK-007
Level 9
January 5, 2016
askdctmAuthor
Level 9
January 5, 2016

Hi Kishore,

I did go through that, but was not very clear.

Any example/thought on it would be helpful.

Lokesh_Shivalingaiah
Level 10
January 5, 2016

one basic difference between these is 'Contains' will search for the given string anywhere in the content, however 'LIKE' will search for the given expression, ex: abc% where it searches for the string abc to be the starting of the content and not in the middle.

GK-007
GK-007Accepted solution
Level 9
January 5, 2016

Please see below.

  • Below Queries will find any node with a title property which contains the text client any where in the text phrase.

            SELECT * FROM [nt:base] AS s WHERE CONTAINS(s.title, 'client')

           SELECT * FROM [nt:base] AS s WHERE s.title like  '%client%'

  •  Query will find files with ends with the string ".png" extension

            SELECT * FROM [nt:file] WHERE NAME() LIKE '%.png'

  • Query will only find the files starts with the name 'abc'

            SELECT * FROM [nt:file] WHERE NAME() LIKE 'abc%'

Hope this clears difference between LIKE and COONTAINS

askdctmAuthor
Level 9
January 5, 2016

Hi Kishore,

Thanks a lot for your reply.

So, as per my understanding, from the above post , the below two queries will perform the same functionality of finding nodes with title property, which contains "client" anywhere in the text phrase?

SELECT * FROM [nt:base] AS s WHERE CONTAINS(s.title, 'client')

SELECT * FROM [nt:base] AS s WHERE s.title like  '%client%'

GK-007
Level 9
January 5, 2016

Yes.

kautuk_sahni
Community Manager
Community Manager
January 6, 2016

Adding to answer what lokesh has mentioned, so that in future this answer would act as better solution,

one basic difference between these is 'Contains' will search for the given string anywhere in the content, however 'LIKE' will search for the given expression, ex: abc% where it searches for the string abc to be the starting of the content and not in the middle.

 

//Expressions could used in Like, whereas contains would only search exact string starting [not end or in between].

Kautuk Sahni
askdctmAuthor
Level 9
January 8, 2016

Hi Kautuk/Lokesh/Kishore,

Thank you for all your help here.