June 26, 2018 at 11:47 am
My Issue:
I want all the records or ( The ones where INCLUDE_ME = 'Y' ) to show up based on the value in the variable @INCLUDE_ME
One one occasion I would like only to see the ones whefre INCLUDE_ME = 'Y'
On another occasion I would like to display all records. I would not mind changing the data type in the column INCLUDE_ME to INT ( if needed )
Ok.. Smart Brains.. Lets see who can help me.
Declare @INCLUDE_ME CHAR(1) = 'Y'
Declare @tab table ( NAME VARCHAR(20), INCLUDE_ME CHAR(1) ) ;
INSERT INTO @tab( NAME, INCLUDE_ME )
Select 'ABC' as NAME , 'Y' as INCLUDE_ME
UNION
Select 'CDC' as NAME , 'N' as INCLUDE_ME
UNION
Select 'XXX' as NAME , 'Y' as INCLUDE_ME
;
SELECt *
FROM
@tab
WHERE
CASE WHEN @INCLUDE_ME = 'Y' THEN 1 ELSE 0 END = 1
June 26, 2018 at 11:54 am
mw_sql_developer - Tuesday, June 26, 2018 11:47 AMMy Issue:
I want all the records or ( The ones where INCLUDE_ME = 'Y' ) to show up based on the value in the variable @INCLUDE_ME
One one occasion I would like only to see the ones whefre INCLUDE_ME = 'Y'
On another occasion I would like to display all records. I would not mind changing the data type in the column INCLUDE_ME to INT ( if needed )Ok.. Smart Brains.. Lets see who can help me.
Declare @INCLUDE_ME CHAR(1) = 'Y'
Declare @tab table ( NAME VARCHAR(20), INCLUDE_ME CHAR(1) ) ;INSERT INTO @tab( NAME, INCLUDE_ME )
Select 'ABC' as NAME , 'Y' as INCLUDE_ME
UNION
Select 'CDC' as NAME , 'N' as INCLUDE_ME
UNION
Select 'XXX' as NAME , 'Y' as INCLUDE_ME
;SELECt *
FROM
@tab
WHERE
CASE WHEN @INCLUDE_ME = 'Y' THEN 1 ELSE 0 END = 1
No need, managed to figure out on my own....Thanks for taking time to read
Declare @INCLUDE_ME INT= 1 ;
Declare @tab table ( NAME VARCHAR(20), INCLUDE_ME INT ) ;
INSERT INTO @tab( NAME, INCLUDE_ME )
Select 'ABC' as NAME , 1 as INCLUDE_ME
UNION
Select 'CDC' as NAME , 0 as INCLUDE_ME
UNION
Select 'XXX' as NAME , 1 as INCLUDE_ME
;
SELECt *
FROM
@tab
WHERE
CASE WHEN @INCLUDE_ME = 1 THEN (INCLUDE_ME/1) ELSE LEN(INCLUDE_ME) END = 1
June 26, 2018 at 11:58 am
Try building a table of truth with the possible options. Once you have it, the statement should be fairly easy to write.
June 26, 2018 at 11:59 am
mw_sql_developer - Tuesday, June 26, 2018 11:54 AMmw_sql_developer - Tuesday, June 26, 2018 11:47 AMMy Issue:
I want all the records or ( The ones where INCLUDE_ME = 'Y' ) to show up based on the value in the variable @INCLUDE_ME
One one occasion I would like only to see the ones whefre INCLUDE_ME = 'Y'
On another occasion I would like to display all records. I would not mind changing the data type in the column INCLUDE_ME to INT ( if needed )Ok.. Smart Brains.. Lets see who can help me.
Declare @INCLUDE_ME CHAR(1) = 'Y'
Declare @tab table ( NAME VARCHAR(20), INCLUDE_ME CHAR(1) ) ;INSERT INTO @tab( NAME, INCLUDE_ME )
Select 'ABC' as NAME , 'Y' as INCLUDE_ME
UNION
Select 'CDC' as NAME , 'N' as INCLUDE_ME
UNION
Select 'XXX' as NAME , 'Y' as INCLUDE_ME
;SELECt *
FROM
@tab
WHERE
CASE WHEN @INCLUDE_ME = 'Y' THEN 1 ELSE 0 END = 1No need, managed to figure out on my own....Thanks for taking time to read
Declare @INCLUDE_ME INT= 1 ;
Declare @tab table ( NAME VARCHAR(20), INCLUDE_ME INT ) ;INSERT INTO @tab( NAME, INCLUDE_ME )
Select 'ABC' as NAME , 1 as INCLUDE_ME
UNION
Select 'CDC' as NAME , 0 as INCLUDE_ME
UNION
Select 'XXX' as NAME , 1 as INCLUDE_ME
;SELECt *
FROM
@tab
WHERE
CASE WHEN @INCLUDE_ME = 1 THEN (INCLUDE_ME/1) ELSE LEN(INCLUDE_ME) END = 1
Can you try to create a more expensive option? That doesn't seem to be slow enough.
Keep it simple and avoid the CASE expression.
June 26, 2018 at 12:28 pm
Lynn Pettis - Tuesday, June 26, 2018 12:12 PMA simple catch all query will work
SELECT *
FROM @tab AS [t]
WHERE ([t].[INCLUDE_ME] = @INCLUDE_ME) OR (@INCLUDE_ME <> 'Y');
Let him think or fail on his own.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply