I am using T-SQL (SQL Server).
This is the starting code:
DECLARE @code as char(8)
SET @code = '123' -- 123%'
DECLARE @lsUseLikeForCodeWhereCondition char(1)
IF CHARINDEX('%', @code) = 0
SET @lsUseLikeForCodeWhereCondition = 'N'
ELSE
SET @lsUseLikeForCodeWhereCondition = 'Y'
Is there any performance between these two statements:
select * from mytable where idn_orn_i LIKE
CASE WHEN @lsUseLikeForCodeWhereCondition = 'N' THEN
@code
ELSE
@code + '%'
END
vs
IF @lsUseLikeForCodeWhereCondition = 'N'
BEGIN
select * from mytable where idn_orn_i = @code
END
ELSE
BEGIN
select * from mytable where idn_orn_i Like @code + '%'
END
Both appear to return the same results. But where it says mytable. It is actually a join with 10 different tables. So it is not small. Mostly I am wondering if the optimizer would recognize a like WITHOUT a percent sign in the string and do an equals.
If it matters idn_orn_i is char(8).