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 DORYFROM ttfext200100;however in SQL i can not get it done. I have the following statementUSE baandb IF (SELECT t_fpln FROM ttfext200100 WHERE t_fpln='351')PRINT 'yes'ELSE PRINT 'no'GOerror is Server: Msg 156, Level 15, State 1, Line 3Incorrect 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 DORYFROM ttfext200100 Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
cardgunner
326 Posts |
Posted - 2007-04-20 : 08:16:50
|
Hey, that worked thanks. Only thing is I have 2 columns t_fpln DORY351 361PR YESI'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 |
 |
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
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')BEGINPRINT 'yes'ELSE PRINT 'no'GO |
 |
|
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' ENDELSE BEGIN PRINT 'no' END |
 |
|
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' ENDELSE BEGIN PRINT 'no' END
No, it won't because you forgot EXISTS keyword after IF.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
kimharlan
Starting Member
9 Posts |
Posted - 2007-04-20 : 09:05:50
|
Yes, you're right. Sorry and thanks! |
 |
|
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 ''.ThanksCard Gunner |
 |
|
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 DORYFROM ttfext200100SQL Server Helperhttp://www.sql-server-helper.com |
 |
|
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 |
 |
|
|