SQL If..Else statement

Status
Not open for further replies.

oldshaghat

Ars Tribunus Militum
2,449
I can't seem to find any references to help me on this, although the list that was posted in the last newbie SQL question was quite helpful. Anyhoo.

Using SQL 7, I have a table with a bit field, and I want to return a column in a view that has text based on that bit. So, any ideas why can't I do this:

Select
If (table.bit = 1)
'foo'
Else
'bar'
As mytext
From table

with my expectation being

table.bit
---------
1
1
0
1
0

returning


mytext
------
foo
foo
bar
foo
bar
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
I assume you mean SQL Server 7. If you look at the syntax for the SELECT statement (which is huge and nasty and not reproduced here), you won't find an if..else clause in it.

Here is a possible solution. I don't have a SQL Server 7 installation handy to test it against, but it should be close:

Code:
SELECT 'foo' AS mytext
FROM mytable t1
WHERE t1.bit = 1
UNION
SELECT 'bar'
FROM mytable t2
WHERE t2.bit = 0

Try that. It's been a couple of years since I've had to write SQL, so that may not be entirely correct, but it should point you in the right direction.
 

reboot

Ars Legatus Legionis
10,818
Augh, please people stop calling "SQL Server" just "SQL".

SQL Server is a RDBMS product from Microsoft.
SQL is the Structured Query Language to access (R)DBMS'.
Every RDBMS uses it's own superset of SQL, under SQL Server it's T-SQL.

Anyways, I was going to suggest decode but that only works under Oracle.
For sure there's an equivalent under SQL Server , it should be more elegant (and efficient??) than a case statement.
 

kleel

Ars Scholae Palatinae
1,022
I just did a quick (read unscientific) test in SQL Server's Query analyser and the additional "compute scalar" step involved only took 0.000001 extra cost with a 0.0384 estimated subtree cost.
Admittedly this is on a small number of rows.

here is a cut&paste from SQL Server Books Online
Migrating DECODE Functions from Oracle to SQL Server
Use the CASE expression to replace the DECODE statement in your application code.

so it seems that they are equivelent :)
 
Originally posted by Jackass JoeJoe:
While we're on the subject, where the hell do you find a syntax reference for SQL _expressions_? Books online doesn't seem to have a quick reference for if's, case statements, and the like.
The MSSQL8 Books Online certainly does.

If you have the current MSDN installed (with the new-style help viewer), you can see them here:
if/else: ms-help://MS.MSDNQTR.2002JAN.1033/tsqlref/ts_ia-iz_7usl.htm
case: ms-help://MS.MSDNQTR.2002JAN.1033/tsqlref/ts_ca-co_5t9v.htm

Also available on-line, I imagine.

It has a reference for all T-SQL keywords, and occasional information on differences between MSSQL7 and 8.

Anyways, I was going to suggest decode but that only works under Oracle.
For sure there's an equivalent under SQL Server , it should be more elegant (and efficient??) than a case statement.
I believe that CASE is the preferred mechanism for this task, and is arguably nicer than decode.
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by Jackass JoeJoe:
Who in the hell picked "control-of-flow" for the topic name over the __very__ commnly used "decision statements"? That section must have been written by a foreigner.
Well, everyone has a different name for them ... control statements, control flow statements, control blocks, decision statements, flow control statements, program flow control .... I usually search for every permutation till I find them.

I've noticed terms involving "control" or "flow" seem to be becoming the preferred terminology.
 
Originally posted by Jackass JoeJoe:
That doesn't look like a quick reference to me.
I think the grammar summary at the top provides just that.

Who in the hell picked "control-of-flow" for the topic name over the very commnly used "decision statements"? That section must have been written by a foreigner.
I've never heard the term "decision statement". "flow control" or "control of flow" are the terms familiar to me.
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by PeterB:
I've never heard the term "decision statement". "flow control" or "control of flow" are the terms familiar to me.
I think "decision statement" is the "old-school" term for them. You know, back when BASIC and FORTRAN were all the rage. ;)

(I actually have heard the term before, but I believe it was back when I was programming in BASIC).
 
Status
Not open for further replies.