Solved

SQL Server conditional Where in select

Posted on 2013-02-05
2
527 Views
Last Modified: 2013-02-05
I have a select that I want to have a conditional where statement

Passing in a variable 'P' or 'B'

I have a table with 4 columns
id    dText     Bill     Pay
1     Hours     1          0
2     Hrs-Reg  0          1

etc

When @var = 'P'
return  data where Pay = 1

when @var = 'B'
return data where Bill = 1

I have this which works...just wondering if I can streamline it
ALTER PROCEDURE usp_HoursTrackingDetailHoursGet2
			(
				@type VARCHAR(1)
			)
AS
BEGIN
	SET NOCOUNT ON;

		IF @type = 'B'
		BEGIN
			SELECT	hsid ,
					Definition 
			FROM	dbo.HoursTracking_SID_Definitions
			WHERE	Bill = 1
			ORDER BY def DESC, sort ASC
		END
		ELSE
		BEGIN
			SELECT	hsid ,
					Definition 
			FROM	dbo.HoursTracking_SID_Definitions
			WHERE	Pay = 1			
			ORDER BY def DESC, sort ASC
		END
END

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 38856903
Copy-paste the below into your SSMS, verify it works how you want, then modify to suit your needs.

CREATE TABLE #foo (id int, dText char(10), bill bit, pay bit)

INSERT INTO #foo (id, dText, bill, pay)
VALUES (1, 'Hours', 1, 0), (2, 'Hrs-Reg', 0, 1)

Declare @var char(1) = 'B'

SELECT id, dText, Bill, Pay
FROM #foo
WHERE (@var = 'P' AND bill=1) OR (@var = 'B' AND pay = 1)

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 38857009
Perfect!

Remind me to buy you a sody-pop sometime
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

910 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

23 Experts available now in Live!

Get 1:1 Help Now