Sql Server Contains

sql, sql-server

Solution

Are you looking for the `LIKE` function? http://www.w3schools.com/sql/sql_like.asp

... WHERE MyColumn LIKE '%AB_SYS%'

That may not be optimal, but it seems like it answers your question... If you can search from only the left or right side that could further optimize.

That is functionally similar to `String.Contains` http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

EDIT: How will you parse the input text into the "relevant" substring?

EDIT: To search the same `LIKE` condition but reverse, from your partial column to the complete literal, simply append the wildcard characters:

... WHERE 'AB_SYS_20120430.TXT' LIKE '%' + MyColumn + '%'

EDIT: You have suggested that you can't get it to work. If you add the schema do your question then I can help you further but consider this:

You have a table called MyTable In that table there is a column called MyColumn Some rows in that table have the data 'AB_SYS' in MyColumn

Given the parameter 'AB_SYS_20120430.TXT' you want to return all matching rows

CREATE PROCEDURE MyTestProcedure 
    @pFullNameString nvarchar(4000) = '' -- parameter passed in, like AB_SYS_20120430.TXT
AS
BEGIN
    SELECT
        *
    FROM
        MyTable
    WHERE
        @pFullNameString LIKE '%' + MyTable.[MyColumn] + '%'
END
GO

Problem

I need to match on a partial string but can't turn full-text indexing on so can't use contains. I've looked at Levenstein's function for determining the distance between two strings but I'm not looking for fuzzy matching but that every character in the column exists in the string. I.e. If the string being passed is something like AB_SYS_20120430.TXT I want to match on any columns containing AB_SYS. The like predicate isn't getting me there. I really need the equivalent of the .NET contains feature but as mentioned turning on full text indexing isn't an option to be turned on. Thought I would see if there were any other possible work arounds.

Original source