I have a view which breaks apart delimited list. For example if you have the following table with the following two columns:
ABC, A;B;C
Assuming the name "TestView", you would get the following:
Label Source
--------------
ABC A
ABC B
ABC C
The code is derived from here: Split one column into multiple rows
When I try to put a condition in the where clause of the view, I'm getting an error. "Invalid length parameter passed to the LEFT or SUBSTRING function." So anything like "select * from TestView where Source = 'A'" will cause that error.
Here's the code from the view:
CREATE VIEW [dbo].[TestView]
AS
WITH L0 AS (SELECT 1 AS c
UNION ALL
SELECT 1),
L1 AS (SELECT 1 AS c
FROM L0 AS A,
L0 AS B),
L2 AS (SELECT 1 AS c
FROM L1 AS A,
L1 AS B),
L3 AS (SELECT 1 AS c
FROM L2 AS A,
L2 AS B),
--counts up to 256
Numbers AS (SELECT ROW_NUMBER() OVER (ORDER BY c) AS n
FROM L3)
SELECT Label,
LTRIM(RTRIM(SUBSTRING(valueTable.Sources, nums.n, charindex(N';', valueTable.Sources + N';', nums.n) - nums.n))) AS Source
FROM Numbers AS nums
JOIN dbo.SourceTable AS valueTable ON nums.n <= CONVERT(int, LEN(valueTable.Sources))
AND SUBSTRING(N';' + valueTable.Sources, n, 1) = N';'