Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use a parameter in SQL to return values > 5 when the parameter is "Yes" and return all values when the parameter is "No"

I don't have a good understanding of how to do this. If you know how to do this then please explain the logic behind it.


Sorry about that. I have updated it to show the code. At the moment it is giving me an error of Incorrect syntax near >

What I have tried:

DECLARE @Param1 NTEXT
DECLARE @Param2 DATETIME
DECLARE @Param3 DATETIME

SELECT TABLE1.Field1
FROM TABLE1(NOLOCK)
WHERE
TABLE1.Field1 = (IIF(@Param1 = 'Yes',TABLE1.Field1 > 3.9, TABLE1.Field1))
AND TABLE1.Field2 BETWEEN @Param2 AND @Param3
Posted
Updated 27-Jul-18 7:38am
v2
Comments
W Balboos, GHB 27-Jul-18 12:52pm    
Is that all you have? One incomplete statement? How are we supposed to know what you are doing and what you have done.

What do you do and and what happens for you to decide this doesn't work?

1 solution

Simple:
SQL
...
WHERE
    (@Param Is Null Or @Param != 'Yes' Or YourField > 5)
...
 
Share this answer
 
Comments
reed_s 27-Jul-18 13:52pm    
That works! Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900