'SQL Switch/Case in 'where' clause with and
I currently have this query
DECLARE @user uniqueidentifier
, @day_to_find int
, @date DATETIME = getdate()
SELECT
@user = CAST(Value AS uniqueidentifier)
FROM BookSoreConfiguration.dbo.Config(NOLOCK)
WHERE Key = 'SuperUser.Id'
SELECT
@day_to_find = CAST(Value AS int)
FROM BookSoreConfiguration.dbo.Config(NOLOCK)
WHERE Key = 'Returns.MaxDay'
--INSERT INTO...... //Part not copied for cleaning
SELECT DISTINCT
@user,
@date,
bd.shopping_code,
bd.cod_gender,
bd.book_number,
'03',
'01',
'11',
@user,
@user,
GETDATE(),
GETDATE(),
ean_code,
15,
bd.cod_variation,
FROM bdo.BOOK_DATA bd WITH (NOLOCK)
-- various inner joins...
WHERE
btw.num_catalog IS NOT NULL
AND(
(
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= @day_to_find
OR (cod_catalog = '04'
AND bd.cod_category NOT IN ('children', 'used', 'discounted'))
AND bd.cod_state= '00'
AND bd.cod_variation = '00'
AND NOT EXISTS (SELECT TOP 1
*
FROM BOOK_RETURNS br (NOLOCK)
WHERE br.code = bd.shopping_code
AND br.cod_category = bd.cod_category
AND br.book_number = bd.book_number
AND br.cod_request_returns = 1
AND br.num_progr_bd = bd.prog
AND br.cod_elaboration = '103')
GO
I need to modify this part:
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= @day_to_find
If bd.shopping_code is "B41", "B74", "BT34" the and must become
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= 30
In all other cases the and will remain
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= @day_to_find
Is it possible to manage the two cases through the switch case or are there other possible ways?
EDIT 2022/05/13 What if I used a UNION?
DECLARE @user uniqueidentifier
, @day_to_find int
, @date DATETIME = getdate()
SELECT
@user = CAST(Value AS uniqueidentifier)
FROM BookSoreConfiguration.dbo.Config(NOLOCK)
WHERE Key = 'SuperUser.Id'
SELECT
@day_to_find = CAST(Value AS int)
FROM BookSoreConfiguration.dbo.Config(NOLOCK)
WHERE Key = 'Returns.MaxDay'
--INSERT INTO...... //Part not copied for cleaning
SELECT DISTINCT
@user,
@date,
bd.shopping_code,
bd.cod_gender,
bd.book_number,
'03',
'01',
'11',
@user,
@user,
GETDATE(),
GETDATE(),
ean_code,
15,
bd.cod_variation,
FROM bdo.BOOK_DATA bd WITH (NOLOCK)
-- various inner joins...
WHERE
bd.shopping_code NOT IN ('B41', 'B74', 'BT34')
AND btw.num_catalog IS NOT NULL
AND(
(
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= @day_to_find
OR (cod_catalog = '04'
AND bd.cod_category NOT IN ('children', 'used', 'discounted'))
AND bd.cod_state= '00'
AND bd.cod_variation = '00'
AND NOT EXISTS (SELECT TOP 1
*
FROM BOOK_RETURNS br (NOLOCK)
WHERE br.code = bd.shopping_code
AND br.cod_category = bd.cod_category
AND br.book_number = bd.book_number
AND br.cod_request_returns = 1
AND br.num_progr_bd = bd.prog
AND br.cod_elaboration = '103')
UNION
SELECT DISTINCT
@user,
@date,
bd.shopping_code,
bd.cod_gender,
bd.book_number,
'03',
'01',
'11',
@user,
@user,
GETDATE(),
GETDATE(),
ean_code,
15,
bd.cod_variation,
FROM bdo.BOOK_DATA bd WITH (NOLOCK)
-- various inner joins...
WHERE
bd.shopping_code IN ('B41', 'B74', 'BT34')
AND btw.num_catalog IS NOT NULL
AND(
(
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= 30
OR (cod_catalog = '04'
AND bd.cod_category NOT IN ('children', 'used', 'discounted'))
AND bd.cod_state= '00'
AND bd.cod_variation = '00'
AND NOT EXISTS (SELECT TOP 1
*
FROM BOOK_RETURNS br (NOLOCK)
WHERE br.code = bd.shopping_code
AND br.cod_category = bd.cod_category
AND br.book_number = bd.book_number
AND br.cod_request_returns = 1
AND br.num_progr_bd = bd.prog
AND br.cod_elaboration = '103')
GO
Solution 1:[1]
Since the left part remains the same and only the value can be different, you could try the following approach (CASE 'returns' the value of the right part):
AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >=
CASE
WHEN bd.shopping_code IN ('B41', 'B74', 'BT34') THEN 30
ELSE @day_to_find
END
Solution 2:[2]
WHERE
(
bd.shopping_code IN ('B41', 'B74', 'BT34') AND DATEDIFF(DAY, btw.sell_date, GETDATE()) >= 30
OR
DATEDIFF(DAY, btw.sell_date, GETDATE()) >= @day_to_find
)
AND ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Stephan Bauer |
Solution 2 | Limonka |