Can any one help me from solving this Dynamic query to a Static one
DECLARE @REPCD VARCHAR(10)
SET @REPCD='CA%'
IF (CHARINDEX('*',@REPCD,1)>0 OR CHARINDEX('%',@REPCD,1)>0)
BEGIN
SET @WHERE = ' AND REP_CD like ''' + REPLACE(@REPCD, '''', '') + ''''
END
ELSE
BEGIN
SET @WHERE = ' AND REP_CD = ''' + REPLACE(@REPCD, '''', '') + ''''
END
I would like to convert the above query to something like this
SELECT * FROM REP
WHERE CASE WHEN CHARINDEX('%',@REPCD,1)>0 THEN REP_CD like 'CA%' END AS REP_CD
Why won’t this work?
In the case where
@REPCDcontains no wildcards,LIKEworks just like=. So there is no reason for aCASEor any other variation of conditional logic at all.There currently is no way to make
LIKE/=interchangeable without dynamic SQL. Well, I guess there is this convoluted way (assuming REP_CD can’t be NULL), but I don’t have any clue why this is better than what I posted above.