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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT..CASE...Question

Status
Not open for further replies.

vbjohn

Programmer
Joined
Aug 23, 2001
Messages
67
Location
US
I was wondering if there is a way that I could do IF Statements in SQL?

My Statement:
SELECT UPR00100.EMPLNAME,
sNStr = CASE WHEN UPR00100.CODE > '1' THEN 'Higher' ELSE 'Lower' END
FROM UPR00100


IF CODE = 1 THEN
IF TXBLWAGS > '2126' THEN
'Statement
ELSE
'Statement
ENDIF
ELSE
'Code = 0
'Statement
ENDIF


Then whatever the outcome is call it 'NewWages


I hope this makes sense.
 

You can use IF statments in SQL but within a Select statment.

Example:

IF @option = 1
BEGIN
SELECT * FROM TableA
END
ELSE
BEGIN
SELECT * FROM TableQ
END

You can NEST CASE statements.

Example:

SELECT
EMPLNAME,
sNStr =
CASE
WHEN CODE > '1' THEN
CASE
WHEN TXBLWAGS > 2126 THEN 'value 1'
WHEN TXBLWAGS > 5000 THEN 'value 2'
ELSE 'value 3' END
WHEN CODE = '0' THEN 'value 4'
ELSE 'value 5' END
FROM UPR00100 Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top