Solved

Return All Values if Filter Item is Left Blank

Posted on 2013-05-20
3
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

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 41

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

688 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