Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Statement in SELECT Query

Status
Not open for further replies.

Mantle51

Programmer
Aug 17, 2006
97
US
Hello,
The following CASE statement works in this Select statement, however I was toiling unsuccessfully for quite some time to try to get an IF statement to accomplish the same thing. Is this not feasible?

SELECT [Evaluation_Date],CASE WHEN [Period_Code] = 'D' THEN 'YES' ELSE 'NO' END AS [Full_End_Date]

muchas gracias.....mickey
 
IF is procedural, that is, execution-flow.

CASE is expressive, that is, evaluative.

Queries are neither expressive nor procedural... call them operative. And you cannot use execution-flow statements in the middle of a query. You can only use evaluative expressions.

Queries can be embedded inside parentheses to become expressive in some contexts.

One exception I can think of to this rule is:

INSERT Table EXEC SPName

But this is a special syntax.

[COLOR=black #d0d0d0]When I walk, I sometimes bump into things. I am closing my eyes so that the room will be empty.[/color]
 
Is this not feasible?

"feasible" is an interesting word. It sort of implies that something can be done but that the cost would be prohibitive.

Usually when we say that something is not feasible, we mean it is possible. But we dont want to do it.

In the Microsoft SQL Server dialect of SQL, there is no such thing as "IF". Oh sure there is an IF statement in Transact-SQL which is the procedural lanuguage used with MS SQL Server. But that is a different thing, isnt it.

So SELECT IF [Period_Code] = 'D' THEN 'YES' ELSE 'NO' is not possible. Even though you want to do it. Therefore it might be feasible. [pipe]



 
Esquared writes,
"CASE is expressive, that is, evaluative. Queries are neither expressive nor procedural... call them operative"

Is not the SQL syntax declarative by nature? So, is that the same as saying queries are operative? Also, is CASE then declarative(as being a subset of expressive for instance)? If so, then why is case used in Procedural programming?
 
Mantle51 said:
Is not the SQL syntax declarative by nature?

I dunno about that, but I've been throwing expletives at it all day....

< M!ke >
I am not a hamster and life is not a wheel.
 
Procedural programming is chock full of expressive statements.

Code:
IF Action + Iteration = "Calc1" BEGIN
   SET X = Power(1 * 2 + 3 / 4, 8 - 7 + 2 * 9) 
END
ELSE BEGIN
   SET X = 42
END

Is not the SQL syntax declarative by nature
Not sure what you mean by this.

In my example above, IF, BEGIN, END, and ELSE are procedural statements. SET and the = on the SET lines are declarative. Everything else is expressive, Action, + , Iteration, "Calc1", X, Power(...), 42.

[COLOR=black #d0d0d0]When I walk, I sometimes bump into things. I am closing my eyes so that the room will be empty.[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top