Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to perform like this but getting error

MY QuERY IS like


select * from TableNm where iif(CAl1='True',(CalStatu1='Done' and CalDis='Pending'),(CalFinal='True'))


GOT ERROR LIKE
Incorrect syntax near '='.


i want like if CAL1 is true then where Condition will check by
CalStatu1='Done' and CalDis='Pending'
otherwise if will check by
CalFinal='True'


Please reply

What I have tried:

select * from TableNm where   iif(CAl1='True',(CalStatu1='Done' and CalDis='Pending'),(CalFinal='True')) 
Posted
Updated 24-Apr-19 22:03pm

You don't need an IIF and you don't need CASE, you just need to get your AND and OR sorted properly e.g.
SQL
select * from TableNm where

(CAl1='True' AND CalStatu1='Done' AND CalDis='Pending')
OR
(Cal1 <> 'True' AND CalFinal='True')
Note the use of brackets to group together the AND criteria. Mixing AND and OR together without brackets is a very common mistake. Always use brackets to clarify your intention.

On the other hand, if you are trying to update columns then you have to do them one at a time and in an Update statement e.g.
SQL
UPDATE TableNm
SET CalStatu1 = CASE WHEN Cal1 = 'True' THEN 'Done' ELSE CalStatu1 END,
	CalFinal = CASE WHEN CAl1 = 'True' THEN CalFinal ELSE 'True' END
 
Share this answer
 
v2
Comments
hareshdgr8 25-Apr-19 4:41am    
Thank You for your help ...
Sir... :-)
CHill60 25-Apr-19 4:53am    
My pleasure
Hi,

I think you may be misinterpreting the Iif command.
The documentation states:
boolean_expression
A valid Boolean expression.

If this argument is not a Boolean expression, then a syntax error is raised.

true_value
Value to return if boolean_expression evaluates to true.

false_value
Value to return if boolean_expression evaluates to false.


Therefore, your syntax
select * from TableNm where   iif(CAl1='True',(CalStatu1='Done' and CalDis='Pending'),(CalFinal='True'))
is incorrect in that the true_value is actually a return value.

It is highly suggestible to change this to a case statement, in order to make your project/query portable with all SQL server instances.

I unfortunately do not have MSSQL installed at current, but perhaps the below will assist;
select * from TableNm where
case cal1 = 'true' 
	then CalStatu1='Done' and CalDis='Pending'
else
	CalFinal='True'
end 




Hope this helps.
 
Share this answer
 
Comments
hareshdgr8 25-Apr-19 1:49am    
no sir same getting error over CalStatu1='Done' and CalDis='Pending'

Hide   Copy Code
Incorrect syntax near '='.
Kevheathfield 25-Apr-19 2:23am    
My apologies, I realized there is syntax I missed. (A bit hard without an IDE)

Please try;
select * from TableNm
where CASE WHEN cal1 = 'true' THEN 
CalStatu1 = 'Done' and cal1 = 'true' else CalFinal = 'True' END
hareshdgr8 25-Apr-19 2:58am    
no sir same error

Incorrect syntax near '='.
Kevheathfield 25-Apr-19 4:12am    
Hmm, I can run this on a MySql instance..
Here is my last ditch attempt, please keep in mind that this is not optimal, but based on the column names in the question, I am under the assumption that this is for a home project.

select * from tablenm
WHERE(cal1 = 'true' AND CalStatu1 = 'Done' and cal1 = 'true')
OR  (cal1 != 'true' AND CalFinal = 'True'));
CHill60 25-Apr-19 4:57am    
WHERE(cal1 = 'true' AND CalStatu1 = 'Done' and cal1 = 'true')
should be
WHERE (CAl1='True' AND CalStatu1='Done' AND CalDis='Pending')
You're testing CAl1 twice

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