• ChrisM@Work (7/23/2013)


    -- why cast [Phone no] AS VARCHAR(MAX)?

    -- why use CHARINDEX? You don't need the character position, you only need to know if

    -- one string exists inside the other.

    SELECT [customer].[Customer name],[customer].[Sl_No],[customer].[Id]

    FROM [company].dbo.[customer]

    WHERE ( Charindex('9000413237',CAST([company].dbo.[customer].[Phone no] AS VARCHAR(MAX)))>0 )

    -- use this instead

    SELECT c.[Customer name],c.[Sl_No],c.[Id]

    FROM [company].dbo.[customer] c

    WHERE c.[Phone no] LIKE '%9000413237%'

    -- then try this, which is SARGable (can use a suitable index)

    SELECT c.[Customer name],c.[Sl_No],c.[Id]

    FROM [company].dbo.[customer] c

    WHERE c.[Phone no] LIKE '9000413237%'

    You have mentioned that using LIKE '9000413237%' is SARGable .There are many articles stating 'abc%' will use index but '%abc%' not.But using LIKE '%9000413237%' also helped me a lot.

    Please see my thread regarding this here