Solved

Return All Values if Filter Item is Left Blank

Posted on 2013-05-20
3
269 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
Comment Utility
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
Comment Utility
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now