Home All Groups Group Topic Archive Search About

IF ELSE in a select statement

Author
19 Aug 2005 3:54 PM
Bruno
Hello friends

Is there any possible to use a if.. else in a select statement?
for example
select ,field_a
         ,field_b
         , if field_a like 'TEST' then field_c else field_d
from table_abc

I know that this is not working but I need somethink like this....
Can anyone help me please???

Author
19 Aug 2005 4:07 PM
Alejandro Mesa
You have to use a "case expression". You can not use flow control as part of
the  select statement.

select ,field_a
         ,field_b
         , case when field_a = 'TEST' then field_c else field_d end as c1
from table_abc


AMB

Show quote
"Bruno" wrote:

> Hello friends
>
> Is there any possible to use a if.. else in a select statement?
> for example
> select ,field_a
>          ,field_b
>          , if field_a like 'TEST' then field_c else field_d
> from table_abc
>
> I know that this is not working but I need somethink like this....
> Can anyone help me please???
>
Author
19 Aug 2005 4:09 PM
Adam Machanic
You need a CASE expression...

select ,field_a
         ,field_b
         , CASE
            WHEN field_a like 'TEST' then field_c
            ELSE field_d
            END AS testField
from table_abc


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--


Show quote
"Bruno" <Br***@discussions.microsoft.com> wrote in message
news:489487A4-D2AB-4DEE-87D7-7A5B52F2A821@microsoft.com...
> Hello friends
>
> Is there any possible to use a if.. else in a select statement?
> for example
> select ,field_a
>          ,field_b
>          , if field_a like 'TEST' then field_c else field_d
> from table_abc
>
> I know that this is not working but I need somethink like this....
> Can anyone help me please???
>
Author
19 Aug 2005 4:09 PM
Andrew J. Kelly
What you want is CASE.  Check out CASE in BooksOnLine for details.

--
Andrew J. Kelly  SQL MVP


Show quote
"Bruno" <Br***@discussions.microsoft.com> wrote in message
news:489487A4-D2AB-4DEE-87D7-7A5B52F2A821@microsoft.com...
> Hello friends
>
> Is there any possible to use a if.. else in a select statement?
> for example
> select ,field_a
>         ,field_b
>         , if field_a like 'TEST' then field_c else field_d
> from table_abc
>
> I know that this is not working but I need somethink like this....
> Can anyone help me please???
>
Author
19 Aug 2005 4:16 PM
Bruno
Thanks to everyone
I appreciate a lot

Show quote
"Andrew J. Kelly" wrote:

> What you want is CASE.  Check out CASE in BooksOnLine for details.
>
> --
> Andrew J. Kelly  SQL MVP
>
>
> "Bruno" <Br***@discussions.microsoft.com> wrote in message
> news:489487A4-D2AB-4DEE-87D7-7A5B52F2A821@microsoft.com...
> > Hello friends
> >
> > Is there any possible to use a if.. else in a select statement?
> > for example
> > select ,field_a
> >         ,field_b
> >         , if field_a like 'TEST' then field_c else field_d
> > from table_abc
> >
> > I know that this is not working but I need somethink like this....
> > Can anyone help me please???
> >
>
>
>
Author
19 Aug 2005 4:11 PM
Chandra
hi
you can use CASE statement instead

--
best Regards,
Chandra
http://chanduas.blogspot.com/
http://www.SQLResource.com/
---------------------------------------



Show quote
"Bruno" wrote:

> Hello friends
>
> Is there any possible to use a if.. else in a select statement?
> for example
> select ,field_a
>          ,field_b
>          , if field_a like 'TEST' then field_c else field_d
> from table_abc
>
> I know that this is not working but I need somethink like this....
> Can anyone help me please???
>

AddThis Social Bookmark Button