Solved

Return All Values if Filter Item is Left Blank

Posted on 2013-05-20
3
272 Views
Last Modified: 2013-05-21
I would like to update a Crystal report with the below subselect.  I have added the following line of code 'and WL.scode = '#sCode#'' to the query below.  When a user enters a value for wl.scode (on the report filter), the code works properly.  However, when a user does not enter a value for wl.scode (on the report filter), the report lists nothing (rather than listing ALL wl.scodes that apply).  How do I write the code to include ALL wl.scodes if a users leaves the wl.scode field blank on the report filter?  Please feel free to ask additional questions if I am not explaining myself clearly (I openly admit to being a self-taught SQL novice)!  Thanks in advance for any and all suggestions!  

select CAST(WLG.iBedroomSize as char(1)) + ' Bedroom' as wlglist,
  WL.sDesc            WLsDesc,
  WLGD.hWlApplic      WLGDhWlApplic,
  WLGD.dtApplied      WLGDdtApplied,
  WLGD.iWLPosition      WLGDiWLPosition

from WlGenDet WLGD, WlGen WLG, WlWaitList WL
where WLGD.hGen = WLG.hMy
  and WL.hProperty = #hProp#
  and WL.scode = '#sCode#'
  and WLG.hWaitList  = WL.hMy
  and WLG.bListArchived = 0
0
Comment
Question by:melikins
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39182056
If the UI is a textbox, then something like...

WHERE ...
  and (WL.scode = '#sCode#' OR WL.scode IS NULL)

If the UI is a combo box, I usually create a UNION query to populate it with an ALL, something like...
SELECT -1 as id, '< ALL >' as scode, 0 as sort_order
UNION ALL 
SELECT id, scode, 1 as sort_order
FROM YourTable
ORDER BY sort_order, scode

Open in new window

Then if the user selects < ALL >, handle it in your T-SQL

WHERE ...
  and (WL.scode = '#sCode#' OR WL.scode  = -1)
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39182070
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39182200
check this.
select CAST(WLG.iBedroomSize as char(1)) + ' Bedroom' as wlglist,
  WL.sDesc            WLsDesc,
  WLGD.hWlApplic      WLGDhWlApplic,
  WLGD.dtApplied      WLGDdtApplied,
  WLGD.iWLPosition      WLGDiWLPosition

from WlGenDet WLGD, WlGen WLG, WlWaitList WL
where WLGD.hGen = WLG.hMy 
  and WL.hProperty = coalesce(nullif(#hProp#,''),WL.hProperty)
  and WL.scode = coalesce(nullif('#sCode#',''),WL.scode)
  and WLG.hWaitList  = WL.hMy
  and WLG.bListArchived = 0

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question