?
Solved

Using a wildcard in combination with the CASE function

Posted on 2014-11-17
6
Medium Priority
?
90 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

770 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