Note that there are some explanatory texts on larger screens.

plurals
  1. POT-SQL LIKE condition on comma-separated list
    primarykey
    data
    text
    <p>Is it possible to write a <code>LIKE</code> condition in T-SQL to match a comma-separated list which includes wildcards to a string. Let me explain further with an example:</p> <p>Say you have the following command separated list of urls in a field:</p> <pre><code>'/, /news/%, /about/' </code></pre> <p>Now here's some examples of strings I'd like to match with the string above:</p> <ol> <li>'/'</li> <li>'/news/'</li> <li>'/news/2/'</li> <li>'/about/'</li> </ol> <p>And here's some strings which would not match:</p> <ol> <li>'/contact/'</li> <li>'/about/me/'</li> </ol> <p>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.</p> <p>In case you are wondering here's how I achieved it using the split function:</p> <pre><code>SELECT Widgets.Id FROM Widgets WHERE (SELECT COUNT(*) FROM [dbo].[Split](Urls, ',') WHERE @Input LIKE Data) &gt; 0 </code></pre> <p>And here's the split function:</p> <pre><code>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 &gt; 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 </code></pre> <p>I'd appreciate it if someone could help. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload