DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Pep Gómez Guest

    Returning a boolean expression in a SELECT clause

    Hi... Im trying to return a boolean expression in a SELECT clause and not
    being capable.
    It seems strange to me that SQL Server can't do that because it can return
    other expressions (arithmetic for example).

    What I want to do is return in a column wheter some expression is True or
    False. For example, imagine I have a table in which I have a column with the
    age of people. I want to return the name of the person and whether this
    person is older than 20.
    What I would do is somthing like:

    SELECT Name, (Age>20) As IsOlder
    FROM People

    But that doesn't work :-(
    Is there any way I can accomplish this? Thank you in advance

    Pep
    pep@ati.es



  2. #2
    Daniel Reber Guest

    Re: Returning a boolean expression in a SELECT clause

    try this:

    select
    name,
    case when Age>20
    then 1
    else 0
    end IsOlder
    from People

    if you are using ADO, it will interpret 1 as True and 0 as False.

    if you want it to come back with "True" or "False" try this

    select
    name,
    case when Age>20
    then 'True'
    else 'False'
    end IsOlder
    from People

    just be aware that the return value is a string. You will need to convert it
    to a boolean at the front end.

    Daniel Reber


    "Pep Gómez" <pep@ati.es> wrote in message news:3bc55a52@news.devx.com...
    > Hi... Im trying to return a boolean expression in a SELECT clause and not
    > being capable.
    > It seems strange to me that SQL Server can't do that because it can return
    > other expressions (arithmetic for example).
    >
    > What I want to do is return in a column wheter some expression is True or
    > False. For example, imagine I have a table in which I have a column with

    the
    > age of people. I want to return the name of the person and whether this
    > person is older than 20.
    > What I would do is somthing like:
    >
    > SELECT Name, (Age>20) As IsOlder
    > FROM People
    >
    > But that doesn't work :-(
    > Is there any way I can accomplish this? Thank you in advance
    >
    > Pep
    > pep@ati.es
    >
    >




  3. #3
    Richard Lalonde Guest

    Re: Returning a boolean expression in a SELECT clause


    Try this
    Boolean can be interpreted as (Yes, No) or (0, 1, -1)

    select employeeID, datediff(yy,birthdate,getdate()) as Age,
    case
    when datediff(yy,birthdate,getdate()) >= 50 then 'Y'
    else 'N'
    end as IsOlder
    from northwind.dbo.employees

    "Pep Gómez" <pep@ati.es> wrote:
    >Hi... Im trying to return a boolean expression in a SELECT clause and not
    >being capable.
    >It seems strange to me that SQL Server can't do that because it can return
    >other expressions (arithmetic for example).
    >
    >What I want to do is return in a column wheter some expression is True or
    >False. For example, imagine I have a table in which I have a column with

    the
    >age of people. I want to return the name of the person and whether this
    >person is older than 20.
    >What I would do is somthing like:
    >
    >SELECT Name, (Age>20) As IsOlder
    >FROM People
    >
    >But that doesn't work :-(
    >Is there any way I can accomplish this? Thank you in advance
    >
    >Pep
    >pep@ati.es
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links