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

Stored Procedure with a SELECT

Status
Not open for further replies.

JoshBN

Programmer
Sep 27, 2001
7
US
I have a Stored procedure that I want to have an 'if' statement in my SELECT statement. Can I do this. Here is an example of what I want to do:

ALTER PROCEDURE
AS
SELECT fFirstName, fLastName, if(fLastName = 'Smith') then 1 as intTest else 2 as intTest, fPhone
FROM tbEmployees

Any help would be greatly appreciated.

 
You can do this via the CASE statement. For example, you could do the following:

SELECT firstName, lastName,
CASE lastName
WHEN 'Smith' THEN 1
ELSE 2
END as intTest,
phone
FROM Employees

I believe this will give you what you are looking for.

Jim

 
Jim,

Thanks a million. That looks like it is exactly what I want to do.
Josh
 
Oops, I need one more thing on here. What if I have something like this:

SELECT fLastName, fFirstName, fDateCreated, CASE fDateCreated WHEN BETWEEN '1/1/2001' AND '1/1/2002' THEN 1
ELSE CASE fDateCreated WHEN > '1/1/2002' then 2 else 3

I can't get this working
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top