Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i select perticular id from table in if else condition
Posted
Comments
_Asif_ 26-Feb-15 2:53am    
What have you tried?
Member 10694442 26-Feb-15 5:26am    
i have a table there have a (open, high, low, close, ref)column n 4000 rows
i want that if close column value is greater then ref value then print buy otherwisw sell

& i want to do this for all 4000 rows
Member 10694442 26-Feb-15 5:26am    
i tried with if else condition but syntax or value is not match
Manoj Sawant 26-Feb-15 3:28am    
Please explain your question in detail.
Member 10694442 26-Feb-15 5:26am    
i have a table there have a (open, high, low, close, ref)column n 4000 rows
i want that if close column value is greater then ref value then print buy otherwisw sell

& i want to do this for all 4000 rows

1 solution

Instead of IF-ELSE you need to use CASE[^]
Here is a CodeProject article on the subject A Simple Use of SQL CASE Expression[^].
Member 10694442 said:
& i want to do this for all 4000 rows
Be aware that if you don't filter your SQL query using a WHERE[^] clause then it will affect every row on the table - more information here[^].

In your case this would work:
SQL
select CASE WHEN [close] > [ref] THEN 'Buy' ELSE 'Sell' END
from YourTable
Notice that I have surrounded the column names with square brackets because close is one of SQL Server's reserved words[^]. It is best practise to avoid using reserved words as column or variable names.
 
Share this answer
 
Comments
Member 10694442 26-Feb-15 6:26am    
i dnt want to select i want to insert buy or sell value
if the value of 10th record of (close)column is greater then 9th record of(refrance) then insert value buy otherwise sell
CHill60 26-Feb-15 6:51am    
You used the word "Print" in your comments above. Where do you want to insert this value???
CHill60 26-Feb-15 7:01am    
Hang on a minute - you want to base this value by comparing each record with the previous record??
Please make your question clear!
Member 10694442 26-Feb-15 7:53am    
i m abeginner so forgive me
CHill60 26-Feb-15 8:00am    
I have added relevant information to your other question and deleted my solution so that the question reappears in the list. Next time try to give as much (relevant) information as possible when asking a question - the fact that you are using Access for example, completely changes the potential solutions

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