Home All Groups Group Topic Archive Search About

Using SELECT and IF statement

Author
3 Mar 2006 6:16 PM
Pasquale
I would to create a query SELECT structured so:

SELECT <field1>, <field2>, IF <condition> BEGIN statement END ELSE
statement, <field3>
FROM <Table>

The <condition> verifies the value of a parameter.

Is it possible? I have executed some proofs unsuccessfully!

Many thanks

Author
3 Mar 2006 6:25 PM
Aaron Bertrand [SQL Server MVP]
You are looking for the CASE expression.



Show quote
"Pasquale" <Pasqu***@discussions.microsoft.com> wrote in message
news:4788BEEA-2C3A-4473-B9C3-0ED10B174516@microsoft.com...
>I would to create a query SELECT structured so:
>
> SELECT <field1>, <field2>, IF <condition> BEGIN statement END ELSE
> statement, <field3>
> FROM <Table>
>
> The <condition> verifies the value of a parameter.
>
> Is it possible? I have executed some proofs unsuccessfully!
>
> Many thanks
>
Author
6 Mar 2006 8:38 AM
Pasquale
I would to execute a subquery by a conditioned manner.
I think that "case" isn't useful for my goal.

Many thanks


Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> You are looking for the CASE expression.
>
>
>
> "Pasquale" <Pasqu***@discussions.microsoft.com> wrote in message
> news:4788BEEA-2C3A-4473-B9C3-0ED10B174516@microsoft.com...
> >I would to create a query SELECT structured so:
> >
> > SELECT <field1>, <field2>, IF <condition> BEGIN statement END ELSE
> > statement, <field3>
> > FROM <Table>
> >
> > The <condition> verifies the value of a parameter.
> >
> > Is it possible? I have executed some proofs unsuccessfully!
> >
> > Many thanks
> >
>
>
>
Author
6 Mar 2006 3:55 PM
Laxmi
Hi Pasquale,
If u post the exact problem or query that u want to get solved, i would be
in a better position to answer u using the case statement.
And I dont think If statement can be used in Select query.
Thanks

Show quote
"Pasquale" wrote:

> I would to execute a subquery by a conditioned manner.
> I think that "case" isn't useful for my goal.
>
> Many thanks
>
>
> "Aaron Bertrand [SQL Server MVP]" wrote:
>
> > You are looking for the CASE expression.
> >
> >
> >
> > "Pasquale" <Pasqu***@discussions.microsoft.com> wrote in message
> > news:4788BEEA-2C3A-4473-B9C3-0ED10B174516@microsoft.com...
> > >I would to create a query SELECT structured so:
> > >
> > > SELECT <field1>, <field2>, IF <condition> BEGIN statement END ELSE
> > > statement, <field3>
> > > FROM <Table>
> > >
> > > The <condition> verifies the value of a parameter.
> > >
> > > Is it possible? I have executed some proofs unsuccessfully!
> > >
> > > Many thanks
> > >
> >
> >
> >
Author
6 Mar 2006 4:01 PM
Aaron Bertrand [SQL Server MVP]
>I would to execute a subquery by a conditioned manner.
> I think that "case" isn't useful for my goal.

Well, IF isn't going to be any better, because it can't be used within a
query.

Please read http://www.aspfaq.com/5006 for information on posting questions
that can be answered.
Author
6 Mar 2006 4:24 PM
--CELKO--
You missed the whole idea of SQL.  It is a declarative language, not a
procedural one.  You cannot next statements inside statements.
However, we do have a CASE **expression**, which like all exprssions,
returns a scalar value.

Oh, you might also want to learn why columns are not fields -- another
fundamental concept.

SELECT <col1>, <col>,
              CASE WHEN  <search condition>
                         THEN <exp_1> ELSE <exp_2>  END AS <col_3>.
              <col_4>
  FROM <Table> ;
Author
6 Mar 2006 4:56 PM
Aaron Bertrand [SQL Server MVP]
Joe, did you mean to reply to me?




Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1141662297.800588.23770@j52g2000cwj.googlegroups.com...
> You missed the whole idea of SQL.  It is a declarative language, not a
> procedural one.  You cannot next statements inside statements.
> However, we do have a CASE **expression**, which like all exprssions,
> returns a scalar value.
>
> Oh, you might also want to learn why columns are not fields -- another
> fundamental concept.
>
> SELECT <col1>, <col>,
>              CASE WHEN  <search condition>
>                         THEN <exp_1> ELSE <exp_2>  END AS <col_3>.
>              <col_4>
>  FROM <Table> ;
>

AddThis Social Bookmark Button