Introduction
Sometimes, depending on business requirements, we need to fetch all records if the search text parameter is blank. We have many ways to satisfy that requirement.
I have the following Employee Table in my SQL Server:
SELECT * FROM EMPLOYEE
CASE 1
Now if we pass a Search Parameter to select records from this table.
DECLARE @SEARCHTEXT VARCHAR(50)
SET @SEARCHTEXT='Ra'SELECT * FROM EMPLOYEE WHERE NAME LIKE '%'+ ISNULL(@SEARCHTEXT, NAME) +'%'
Now set SearchText to null or blank.
DECLARE @SEARCHTEXT VARCHAR(50)
SET @SEARCHTEXT=''SELECT * FROM EMPLOYEE WHERE NAME LIKE '%'+ ISNULL(@SEARCHTEXT, NAME) +'%'
Case 2
DECLARE @SEARCHTEXT VARCHAR(50)
SET @SEARCHTEXT=''IF(LEN(@SEARCHTEXT)>0)
SELECT * FROM EMPLOYEE WHERE NAME=@SEARCHTEXT
ELSE
SELECT * FROM EMPLOYEE WHERE NAME= NAME
Summary
This article taught us how Return All Records If the Search Text is Blank in SQL Server.


Rahul Kumar SaxenaTOP 100Posted Apr 23, 2015, 10:13 AM EDT
Thanks Karthik Muthu Karuppan...
Karthik Muthu KaruppanPosted Apr 23, 2015, 7:19 AM EDT
good one
Rahul Kumar SaxenaTOP 100Posted Apr 22, 2015, 10:29 AM EDT
Thanks Namit gupta...
Rahul Kumar SaxenaTOP 100Posted Apr 22, 2015, 10:29 AM EDT
Thanks Nitin tyagi...
Rahul Kumar SaxenaTOP 100Posted Apr 22, 2015, 10:29 AM EDT
Thanks Manoj Kulkarni...
NAMIT GUPTAPosted Apr 22, 2015, 2:10 AM EDT
good one !
NitinPosted Apr 22, 2015, 1:53 AM EDT
Nice
Manoj KulkarniPosted Apr 22, 2015, 12:06 AM EDT
Thank you for sharing
Rahul Kumar SaxenaTOP 100Posted Apr 21, 2015, 7:41 PM EDT
Thanks Gowtham Rajamanickam
Gowtham RajamanickamTOP 100Posted Apr 21, 2015, 7:26 PM EDT
good one