?
Solved

Return All Values if Filter Item is Left Blank

Posted on 2013-05-20
3
Medium Priority
?
275 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 2000 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

777 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