Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LIKE within CASE in TSQL

I would like to select the records that contain the content of the @selctDescription parameter but only when @selctDescription is not empty.

I have the following, which does not work:

(t.[description] LIKE 
  (
  CASE 
  WHEN @selctDescription = '' THEN t.[description] 
  ELSE ('%' @selctDescription '%') 
  END
  )
)

Can anyone point me in the right direction?

like image 993
CiccioMiami Avatar asked Feb 22 '26 23:02

CiccioMiami


1 Answers

SELECT * 
FROM Table
WHERE 
  ((@selctDescription IS NULL OR @selctDescription = '') 
   OR
   (t.[description] LIKE '%' + @selctDescription +'%'))
like image 133
sll Avatar answered Feb 25 '26 14:02

sll