Question about customizing the output of a query...

Question about customizing the output of a query...

Post by Bill Bruc » Wed, 20 Mar 2002 08:37:43



I need to run a query and add to the result set an additional column with
contents that will be derived from the fields in the table(s) I am querying.
For example,

TABLEA has FIELD1, FIELD2, FIELD3 and I want to select FIELD1 and FIELD2,
and also CustomField1 wich will be ValueA if FIELD1 is like 'var_a' and
ValueB if FIELD1 is like 'var_b' and ValueC if FIELD1 is like 'var_c' and so
on.

I have several conditions that I would like to apply to the data to get my
output.  Any ideas?

thanks,

-BB

 
 
 

Question about customizing the output of a query...

Post by Tom Morea » Wed, 20 Mar 2002 08:50:22


It would help if you had DDL with INSERT statements plus exact expected
results.  Here's a guess:

select
    FIELD1
,    FIELD2
,    FIELD3
,    CASE
        WHEN FIELD1 LIKE 'var_a' THEN ValueA
        WHEN FIELD1 LIKE 'var_b' THEN ValueB
        WHEN FIELD1 LIKE 'var_b' THEN ValueC
    END
FROM
    MyTable

Also, I don't know if you really want to use a LIKE predicate or you has
something more specific in mind.

--
   Tom

----------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCT
SQL Server MVP
Columnist, SQL Server Professional

Toronto, ON Canada
www.pinnaclepublishing.com/sql
www.apress.com
---
I need to run a query and add to the result set an additional column with
contents that will be derived from the fields in the table(s) I am querying.
For example,

TABLEA has FIELD1, FIELD2, FIELD3 and I want to select FIELD1 and FIELD2,
and also CustomField1 wich will be ValueA if FIELD1 is like 'var_a' and
ValueB if FIELD1 is like 'var_b' and ValueC if FIELD1 is like 'var_c' and so
on.

I have several conditions that I would like to apply to the data to get my
output.  Any ideas?

thanks,

-BB