Below IMAGE is the DB TABLE.
I want to add an if else statement below the where statement.
Can I change it like below??
I want to add an if else statement below the where statement.
SQL:
ALTER PROCEDURE [dbo].[usp_PROCEDURE]
@startdate varchar(8) = NULL,
@enddate varchar(8) = NULL
AS
BEGIN
.
.
wher id = dm.id
and
if ( @startdate == NULL && @enddate == NULL)
{
CR IS NULL AND UP IS NULL
}
else if ( @startdate == NULL && @enddate == 2023)
{
CR IS NULL AND UP = '2023'
}
else if ( @startdate == 2023 && @enddate == NULL)
{
CR = '2023' AND UP IS NULL
}
else if ( @startdate == 20230101 && @enddate == 20230505)
{
(( CONVERT(DATE, dm.CR) BETWEEN CONVERT(DATE,@startdate) AND (CONVERT(DATE, @enddate))) or ( CONVERT(DATE, dm.UP) BETWEEN CONVERT(DATE, @startdate) AND (CONVERT(DATE, @enddate))))
}
and id.name = a.name
END
Can I change it like below??
Code:
ERROR ===> then CR is null and UP is null
SQL:
and CASE WHEN @startdate is null and @enddate is null then CR is null and UP is null
and CASE WHEN @startdate is null and @enddate is not null then CR is null and UP = @enddate
and CASE WHEN @startdate is not null and @enddate is null then CR = @startdate and UP is null
and CASE WHEN @startdate is not null and @enddate is not null then (( CONVERT(DATE, CR) BETWEEN CONVERT(DATE,@startdate) AND (CONVERT(DATE, @enddate))) or ( CONVERT(DATE, UP) BETWEEN CONVERT(DATE, @startdate) AND (CONVERT(DATE, @enddate))))
Last edited by a moderator: