Solved

Using a wildcard in combination with the CASE function

Posted on 2014-11-17
6
85 Views
Last Modified: 2014-11-22
I've recently started to try and transition a multitude of complex Access databases to an SQL backend due to performance issues. It's my mission to fully utilize a "thin client" approach. In doing so, I'm running into a wall.

As a little background, i've built a table that i'll use to send all of my settings to that views will pull from for filtering with the intent of having a final data set sent back to Access that won't require additional querying. This table is "tblUsers_Settings".

In the Access front end, I have multiple combo boxes that when selected, are used to filter. If they are left blank, I want all records returned. The following filter works as long as the 'rPROID' has a value. If it has no value, I get no results, whereas I need to get all results if 'rPOID' has no value (basically no filter criteria was selected). I've temporarily put in the value 66 as a place holder. I've tried using '%', like '%', and a few other misc. attempts with no results.

Any suggestions on what the 66 should be replaced with to achieve the desired results? Is there a method better than CASE that is more appropriate?

= CASE WHEN (SELECT uValue FROM dbo.tblUsers_Settings AS tblUsers_Settings_2 WHERE (uUser = USER_NAME()) AND (uSetting = 'rPROID')) = '' THEN 66 ELSE (SELECT uValue FROM dbo.tblUsers_Settings AS tblUsers_Settings_2 WHERE (uUser = USER_NAME()) AND (uSetting = 'rPROID')) END
0
Comment
Question by:Level7Sensei
[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
  • 4
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40448619
>all of my settings to that views will pull from for filtering
Filtering implies a WHERE clause, whereas CASE is used mostly in SELECT to change a value based on other conditions.

>The following filter works as long as the 'rPROID' has a value. If it has no value,
In a query's WHERE clause, with the rPROID value as a parameter @rPROID, that would be ...
SELECT blah, blah, blah
FROM tblUsers_Settings
WHERE (rPROID = @rPROID OR @rPROID IS NULL) 

Open in new window


Couple of articles that may help you:

1.

SQL Server CASE Solutions, with a wompload of images and sample code.

2.

Migrating your Access Queries to SQL Server Transact-SQL, which is a grab-bag of things to know when rewriting Access queries in SQL Server.
0
 

Author Comment

by:Level7Sensei
ID: 40448662
I think I need to clarify.

Looking up the rPROID value doesn't give an error. There is always a value, but that value could be a number or it could be an empty '' value.

I'm only loading the rPROID value so that it can be used as a filter in primary query. For example, if rPROID returns "A", i want all results matching "A". If rPROID returns '', i want all results with no filtering.

Not sure what you mean by "@rPROID" - not familiar enough with SQL yet.

Also might be mixing up terminology, so I've attached a screenshot.
Capture.PNG
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40448697
> i want all results matching "A"
Define 'matching'.  column_name = "A", or column_name that contains an "A" in it?
SELECT blah, blah, blah
FROM tblUsers_Settings
WHERE (rPROID LIKE '%' + @rPROID + '%' OR @rPROID IS NULL) 

Open in new window


>Not sure what you mean by "@rPROID" - not familiar enough with SQL yet.
You're going to have to get there quick.  Parameters are basically the same as [Enter your date here] constructs in SQL Server, where Access can call what's called a Stored Procedure, which can handle multiple queries, temp tables, etc. and return a set.  Parameters are basically variables that can be passed into the SP/query.  

For example, if the below were executed in SQL Server to create a stored procedure, running   exec customers_by_state 'NV' would return all the sales where the customer is in Nevada.  

CREATE PROC customers_by_state(@state varchar(2)) AS

SELECT *
FROM Sales s
   JOIN Customer c ON s.customer_id = c.id
WHERE c.state = @state
GO

Open in new window


SQL Server is a database only and does not have the ability to interact with forms like Access does, by including sql like [Forms]![MyFormName]![MyFieldName], so values are passed by using parameters.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Level7Sensei
ID: 40448718
PROID is a column (attribute), as demonstrated in the attached screenshot.
rPROID is a report value derived from the "Select uValue FROM..." that is used to filter PROID.

If rPROID = "A", i need all records where attribute PROID contains the value "A"

If rPROID = '', i need all records regardless of their value.
0
 

Accepted Solution

by:
Level7Sensei earned 0 total points
ID: 40448794
OK I figured it out. Below works as needed. Thanks for the try.

= CASE
WHEN (SELECT uValue FROM dbo.tblUsers_Settings WHERE (uUser = USER_NAME()) AND (uSetting = 'rPROID')) = '' THEN (SELECT tblQuality_Main.PROID 'like %')
ELSE (SELECT uValue FROM dbo.tblUsers_Settings WHERE (uUser = USER_NAME()) AND (uSetting = 'rPROID'))
END
0
 

Author Closing Comment

by:Level7Sensei
ID: 40459132
The solution lied in the syntax of the "if blank" component of the CASE statement. I had to define the full table+criteria to do this, rather than only define the filter criteria.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

717 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