November 24, 2016 at 6:49 am
hi
I've created this query for my ssrs report but i''m getting the error:
Invalid length parameter passed to the LEFT or SUBSTRING function
I want to be able to select multiple values for the @shift parameter.
DECLARE @Shifts TABLE
(
code VARCHAR(10)
,Description VARCHAR(100)
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-A'
,'Morning Shift ( Film18H, Fed2L)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-B'
,'Morning Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-C'
,'Afternoon Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-D'
,'Afternoon Shift (FMF/SVP)'
);
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-H', 'Night Shift' );
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-G', 'Afternoon Shift' );
SELECT
code
,Description AS Shifts
FROM
@Table AS s
WHERE
s.Description IN (
CASE WHEN s.Description LIKE '%(%'
THEN LEFT(s.Description, CHARINDEX('(', @shift) - 1)
ELSE @shift
END );
November 24, 2016 at 6:56 am
Nomvula (11/24/2016)
hiI've created this query for my ssrs report but i''m getting the error:
Invalid length parameter passed to the LEFT or SUBSTRING function
I want to be able to select multiple values for the @shift parameter.
DECLARE @Shifts TABLE
(
code VARCHAR(10)
,Description VARCHAR(100)
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-A'
,'Morning Shift ( Film18H, Fed2L)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-B'
,'Morning Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-C'
,'Afternoon Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-D'
,'Afternoon Shift (FMF/SVP)'
);
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-H', 'Night Shift' );
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-G', 'Afternoon Shift' );
SELECT
code
,Description AS Shifts
FROM
@Table AS s
WHERE
s.Description IN (
CASE WHEN s.Description LIKE '%(%'
THEN LEFT(s.Description, CHARINDEX('(', @shift) - 1)
ELSE @shift
END );
Can you correct your code please, table variable @Table not declared and scalar variable @shift declared as table.
😎
November 24, 2016 at 7:06 am
noted thanks :
DECLARE @Table TABLE
(
code VARCHAR(10)
,Description VARCHAR(100)
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-A'
,'Morning Shift ( Film18H, Fed2L)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-B'
,'Morning Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-C'
,'Afternoon Shift (Film18H)'
);
INSERT INTO @Table
( code
,Description
)
VALUES
( 'CTN-D'
,'Afternoon Shift (FMF/SVP)'
);
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-H', 'Night Shift' );
INSERT INTO @Table
( code, Description )
VALUES
( 'CTN-G', 'Afternoon Shift' );
SELECT
code
,Description AS Shifts
FROM
@Table AS s
WHERE
s.Description IN (
CASE WHEN s.Description LIKE '%(%'
THEN LEFT(s.Description, CHARINDEX('(', @shift) - 1)
ELSE @shift
END );
November 25, 2016 at 12:39 am
November 25, 2016 at 1:25 am
Here is a suggestion
😎
DECLARE @shift VARCHAR(100) = 'Afternoon Shift (FMF/SVP)';
DECLARE @Table TABLE
(
code VARCHAR(10)
,Description VARCHAR(100)
);
INSERT INTO @Table
( code ,Description)
VALUES
( 'CTN-A','Morning Shift ( Film18H, Fed2L)')
,( 'CTN-B','Morning Shift (Film18H)' )
,( 'CTN-C','Afternoon Shift (Film18H)' )
,( 'CTN-D','Afternoon Shift (FMF/SVP)' )
,( 'CTN-H', 'Night Shift' )
,( 'CTN-G', 'Afternoon Shift' );
SELECT
code
,SUBSTRING(s.Description,1,ISNULL(NULLIF(CHARINDEX(' (',Description,1),0),LEN(Description))) AS XShifts
,Description AS Shifts
FROM
@Table AS s
WHERE
@shift LIKE s.Description + '%';
Output
code XShifts Shifts
---------- ---------------- --------------------------
CTN-D Afternoon Shift Afternoon Shift (FMF/SVP)
CTN-G Afternoon Shift Afternoon Shift
November 25, 2016 at 3:31 am
Thanks for the response, but I tried that solution it's only works if you select single value. I you select multiple selection the WHERE clause doesn't work.
Regards,
November 25, 2016 at 7:07 am
Nomvula (11/25/2016)
Yes, I want to be able to enter multiple values for the @shift.
@shift is a scalar variable, you are probably better of splitting the content or do some other tricks to make this work.
😎
This kind of works though
DECLARE @shift VARCHAR(100) = 'Afternoon Shift (FMF/SVP),Morning Shift ( Film18H, Fed2L),Night Shift';
DECLARE @Table TABLE
(
code VARCHAR(10)
,Description VARCHAR(100)
);
INSERT INTO @Table
( code ,Description)
VALUES
( 'CTN-A','Morning Shift ( Film18H, Fed2L)')
,( 'CTN-B','Morning Shift (Film18H)' )
,( 'CTN-C','Afternoon Shift (Film18H)' )
,( 'CTN-D','Afternoon Shift (FMF/SVP)' )
,( 'CTN-H', 'Night Shift' )
,( 'CTN-G', 'Afternoon Shift' );
SELECT
code
,SUBSTRING(s.Description,1,ISNULL(NULLIF(CHARINDEX(' (',Description,1),0),LEN(Description))) AS XShifts
,Description AS Shifts
FROM
@Table AS s
WHERE
@shift LIKE '%' + s.Description + '%';
Output
code XShifts Shifts
---------- ----------------- --------------------------------
CTN-A Morning Shift Morning Shift ( Film18H, Fed2L)
CTN-D Afternoon Shift Afternoon Shift (FMF/SVP)
CTN-H Night Shift Night Shift
CTN-G Afternoon Shift Afternoon Shift
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply