define Value Labels in a Result Set?

Is there a SQL statement that I can use which allows me to set value lables in a result set For example, if a table has a gender variable with values of 'M' & 'F', can I write a sql statement which creates a result set that changes these to 'Male' & 'Female' so that everything I do with the result set shows the new labels (but does not change the underlying M/F values in the orginal table

I do not want to create a new table, but rather display the M/F values as Male/Female when I access the result set in an application (such as Report Services).

Thanks for any advice you can give.

Ed Sloat




Answer this question

define Value Labels in a Result Set?

  • damonh78

    Yes, of course. How embarrassing. You can tell I am fairly new at SQL and I forgot about the case statement. Thanks so much for your reply. That's exactly what I needed.

    All the best.

    Ed Sloat



  • manus_eiffel

    you mean values change not labels . how about using CASE clause like this

    Using CASE

    The CASE function is a special Transact-SQL expression that allows an alternative value to be displayed depending on the value of a column. This change in data is temporary; therefore, there are no permanent changes to the data. For example, the CASE function can display California in a query result set for rows that have the value CA in the state column.

    The CASE function consists of:

    • The CASE keyword.

    • The column name to transform.

    • WHEN clauses specifying the expressions to search for and THEN clauses specifying the expressions to replace them with.

    • The END keyword.

    • An optional AS clause defining an alias for the CASE function.

    This example displays, in the query result set, the full name of the state each author lives in:

    SELECT au_fname, au_lname, 
      CASE state
       WHEN 'CA' THEN 'California'
       WHEN 'KS' THEN 'Kansas'
       WHEN 'TN' THEN 'Tennessee'
       WHEN 'OR' THEN 'Oregon'
       WHEN 'MI' THEN 'Michigan'
       WHEN 'IN' THEN 'Indiana'
       WHEN 'MD' THEN 'Maryland'
       WHEN 'UT' THEN 'Utah'
        END AS StateName
    FROM pubs.dbo.authors
    ORDER BY au_lname
    


  • define Value Labels in a Result Set?