Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 IF STATEMENT

Author  Topic 

cardgunner

326 Posts

Posted - 2007-04-20 : 08:03:13
1st time poster. 1 month in learning SQL the hard way. Trial and error.
I'm trying to write an if statement where as if the selected result then show me yes if not then no. Actaully my desired result would be if not then "". I have done this in MS Access easily.
SELECT ttfext200100.t_fpln, IIf([ttfext200100]![t_fpln]="361PR","YES","") AS DORY
FROM ttfext200100;
however in SQL i can not get it done. I have the following statement

USE baandb
IF (SELECT t_fpln FROM ttfext200100 WHERE t_fpln='351')
PRINT 'yes'
ELSE PRINT 'no'
GO

error is
Server: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'print'.

I tried using begin and end and put in commas and at least 50 different versions of this statement. No luck. Any help?

I tried searching the forum but the search ignores if and else.

Thanks, Cardgunner

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-04-20 : 08:09:27
In SQL you will have to use CASE statement:

select
t_fpln,
CASE t_fpln When '361PR' then 'YES' else '' end AS DORY
FROM ttfext200100


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

cardgunner

326 Posts

Posted - 2007-04-20 : 08:16:50
Hey,
that worked thanks.
Only thing is I have 2 columns
t_fpln DORY
351
361PR YES
I'm sure I can get it to one column.
What does CASE do? Every help article online i found never had it in it.

Again big help thanks.
Cardgunner
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-04-20 : 08:22:47
CASE is similar to IIF() function in Access in terms of functionality. Have you tried SQL Server help for CASE statement?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

kimharlan
Starting Member

9 Posts

Posted - 2007-04-20 : 08:34:48
You can also use Begin and End with an If statement. The following should work fine:

IF (SELECT t_fpln FROM ttfext200100 WHERE t_fpln='351')
BEGIN
PRINT 'yes'
ELSE PRINT 'no'
GO
Go to Top of Page

kimharlan
Starting Member

9 Posts

Posted - 2007-04-20 : 08:36:24
Sorry I hit enter in the midst of typing back!!!

You can also use Begin and End with an If statement. The following should work fine:
IF (SELECT t_fpln FROM ttfext200100 WHERE t_fpln='351')
BEGIN
PRINT 'yes'
END
ELSE
BEGIN
PRINT 'no'
END
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-04-20 : 08:44:35
quote:
Originally posted by kimharlan

Sorry I hit enter in the midst of typing back!!!

You can also use Begin and End with an If statement. The following should work fine:
IF (SELECT t_fpln FROM ttfext200100 WHERE t_fpln='351')
BEGIN
PRINT 'yes'
END
ELSE
BEGIN
PRINT 'no'
END



No, it won't because you forgot EXISTS keyword after IF.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

kimharlan
Starting Member

9 Posts

Posted - 2007-04-20 : 09:05:50
Yes, you're right. Sorry and thanks!
Go to Top of Page

cardgunner

326 Posts

Posted - 2007-04-20 : 09:15:13
Yes Kim I did try that previously in about 50 different ways. That is why i posted. I ferlt i exhuasted everything. It's funny because I in all my searching there was not a CASE statement.

Another quickie. If I wanted to add more then one say '301 or '361PR' how would that look.

So if t_fpln is 301 or 361PR or 351 then yes if not then ''.

Thanks
Card Gunner
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2007-04-20 : 09:24:20
If you have other values to check, your query will look like this:

select
t_fpln,
CASE WHEN t_fpln IN ('361PR', '301', '351') then 'YES' else '' end AS DORY
FROM ttfext200100

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

cardgunner

326 Posts

Posted - 2007-04-20 : 09:29:14
Thanks again I forgot the IN.

I must say I'm impressed with the help. I'll try not to abuse it.

Card Gunner
Go to Top of Page
   

- Advertisement -