Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rwlopez
Yak Posting Veteran
80 Posts |
Posted - 2006-12-18 : 14:48:18
|
| I have a scenerio where I need to alter a section of a WHERE Clause depending on the input I receive from a user, but I am having trouble. This is what I am trying to do can anyone suggest an alternative way of accomplishing this. The syntax below will not work I am only putting it an example of what I need to do. The bottom line is that I need the criteria of the query to change depending on user input.DECLARE @Value AS intSET @Value = 'I'SELECT *FROM SalesPeopleWHERE CASE WHEN @Value = 'I' THEN SalesOpenDate > ‘1/1/2006’ WHEN @Value = 'N' THEN SalesCloseDate < ‘1/1/2006’ |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-12-18 : 16:07:23
|
| Also,There are several articles posted here about "dynamic Where" clauses. Try a site search for more ideas. one possibility:select * from salespeoplewhere salesclosedate > case when @value = 'i' then '1/1/2006' else '1/1/1900' endand salesclosedate < case when @value = 'n' then '1/1/2006' else '1/1/3000' endBe One with the OptimizerTG |
 |
|
|
rwlopez
Yak Posting Veteran
80 Posts |
Posted - 2006-12-18 : 16:18:51
|
| I was going to take the approach with the two SELECT statements, but I am actually using SELECT statement that is fairly long and I didn't want to have duplicate or nearly duplicate code. I also tried to accomplish this by using the second approach mentioned, but it was a little problematic, although I think that I can make it work. I used to do stuff like this in VB all the time with SQL strings, but those SQL statement were just strings. I would like to something like this, but I am not sure if there is a way to make it work. Anyone have any ideas?DECLARE @Value AS intDECLARE @Text AS varchar(100)SET @Value = 'I'CASE WHEN Value = 'I'THEN @Text = 'SalesOpenDate > ‘1/1/2006’'WHEN Value = 'N'THEN @Text = 'SalesCloseDate < ‘1/1/2006’SELECT *FROM SalesPersonWHERE @Text |
 |
|
|
rwlopez
Yak Posting Veteran
80 Posts |
Posted - 2006-12-18 : 16:31:09
|
| Actually I think I found a post that will help me do what I want to do. Thanks for all of your help. |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-19 : 10:05:13
|
| Care to share the link you've found?Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|