I have a stored procedure and I want to allow * in it so that if user:
types t* then taller, tea returns types * or . then all results are returned types t then all results which have t are returned like ptv, tall, sit
my stored procedure is this:
ALTER PROCEDURE [dbo].[SearchEntityDataNew] @SearchText varchar(100) = '*'
AS BEGIN SELECT 'Entity' as pagetype,EntityID,EntityData,EntityDataID FROM EntityData WHERE EntityData LIKE '%' + @SearchText+ '%' OR @SearchText = '*'
union all
SELECT 'Property' as pagetype,PropertyID,PropertyValue,EntityDataID FROM EntityDataProperty WHERE PropertyValue LIKE '%' + @SearchText+ '%' OR @SearchText = '*' END
Maybe something like this would work.. Although sunitabeck's response looks simpler..
ALTER PROCEDURE test.[SearchEntityDataNew] @SearchText varchar(100)
AS BEGIN IF @SearchText = '*' OR @SearchText = '.' BEGIN SELECT myfield FROM mytable END IF RIGHT(@SearchText ,1) = '*' BEGIN SELECT myfield FROM mytable WHERE myfield LIKE LEFT(@SearchText, LEN(@SearchText) - 1) + '%' END IF RIGHT(@SearchText ,1) <> '*' BEGIN SELECT myfield FROM mytable WHERE myfield LIKE '%' + SearchText + '%' END END