Is it possible to write a LIKE condition in T-SQL to match a comma-separated list which includes wildcards to a string. Let me explain further with an example:
Say you have the following command separated list of urls in a field:
'/, /news/%, /about/'
Now here’s some examples of strings I’d like to match with the string above:
- ‘/’
- ‘/news/’
- ‘/news/2/’
- ‘/about/’
And here’s some strings which would not match:
- ‘/contact/’
- ‘/about/me/’
I’ve achieved this in the past by writing a split function and then doing a like on each one. However I’m trying to get my query to work in SQL Server CE which doesn’t support functions.
In case you are wondering here’s how I achieved it using the split function:
SELECT Widgets.Id
FROM Widgets
WHERE (SELECT COUNT(*) FROM [dbo].[Split](Urls, ',') WHERE @Input LIKE Data) > 0
And here’s the split function:
CREATE FUNCTION [dbo].[Split]
(
@RowData NVARCHAR(MAX),
@Separator NVARCHAR(MAX)
)
RETURNS @RtnValue TABLE
(
[Id] INT IDENTITY(1,1),
[Data] NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @Iterator INT
SET @Iterator = 1
DECLARE @FoundIndex INT
SET @FoundIndex = CHARINDEX(@Separator, @RowData)
WHILE (@FoundIndex > 0)
BEGIN
INSERT INTO @RtnValue ([Data])
SELECT Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))
SET @RowData = SUBSTRING(@RowData, @FoundIndex + DATALENGTH(@Separator) / 2, LEN(@RowData))
SET @Iterator = @Iterator + 1
SET @FoundIndex = CHARINDEX(@Separator, @RowData)
END
INSERT INTO @RtnValue ([Data])
SELECT Data = LTRIM(RTRIM(@RowData))
RETURN
END
I’d appreciate it if someone could help. Thanks
I can think of several options:
Use a session-keyed table: delete rows matching current spid, insert desired rows with current spid, read from table in SP, delete from table (again).
Make your client submit a query with many OR … LIKE … clauses.
Write an SP that does the same thing as your function and returns a recordset.
INSERT YourTable EXEC SP @Stringsand you are done!Use the numbers-table-charindex-into-string inside of a derived table method of splitting the string.
Example
Let me flesh this out a little for you with an example combining ideas #3 and #4. Of course, your code for your function could be adapted, too.
Build a separate
Numberstable. Here is example creation script:The SP:
And usage, easy as pie:
Result:
And finally, if you don’t want to build a numbers table, you can use this SP. I think you will find that one of these two SPs performs well enough for you. There are other implementations of string splitting that could work as well.
Any SQL writer serious about understanding some of the performance implications of splitting strings different ways ought to see Aaron Bertrand’s blog post on splitting strings.
Also, any serious SQL Server database student ought to see Erland Sommarskog’s How to Share Data between Stored Procedures.