Selecting distinct values from one column with casting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pb2000
    New Member
    • Apr 2010
    • 13

    Selecting distinct values from one column with casting

    Hello,

    I am beginner in MSSQL.

    After writing:
    Code:
     
    SELECT 
    DISTINCT UpDate AS ExpirationDate
    FROM MyTable
    I have the following result:

    ExpirationDate
    ------------------------------
    1 NULL
    2 2007-11-05 12:53:12.000
    3 2003-01-14 13:33:12.000
    4 2004-01-15 12:34:23.000

    But I would like to obtain:

    ExpirationDate
    -----------------------------
    1 ALIVE
    2 DEAD
    3 ANY

    Namely, I would like to check ISNULL. If there is any record with value NULL then I cast it to string "ALIVE".
    When there is 1 or more dates I would obtain only 1 record with casted to string "DEAD"
    Additionally DB should answer always with record "ANY".

    I thougth with Casting all the dates to BIT if they are NULL or not and then select Distinct values from 0s and 1s.

    Could You help me? Thank You in advance.
    Paul
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I can only think of using CASE. Not too bad if there is only the three options.
    Code:
    SELECT CASE WHEN update ISNULL THEN "ALIVE"
    WHEN COUNT(update) > 1 THEN "DEAD"
    ELSE "ANY" AS ExpirationDate END
    FROM MyTable

    Comment

    Working...