|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using SELECT and IF statementI 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 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 > 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 > > > > > 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 > > > > > > > > > >I would to execute a subquery by a conditioned manner. Well, IF isn't going to be any better, because it can't be used within a > I think that "case" isn't useful for my goal. query. Please read http://www.aspfaq.com/5006 for information on posting questions that can be answered. 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> ; 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> ; > |
|||||||||||||||||||||||