Friday, February 24, 2012

Creating a column.

Is there a way for me to create a column in SQL?

What Im looking to do is create a view with columns from a specific table. In addition to this I would like to append a column to the view which would contain data based on the data from a pre-existing column.

Table A
Column 1
Column 2
Column 3

View A
Column 1
Column 2
Column 3
Column 4

Now here is the Row structure

Column 1 Column 2 Column 3 Column 4
Test Test A ON
Test Test B OFF

So if Column 3 contains "A" as its field data than Column 4 will contain "ON" and if Column 3 contains "B" than Column 4 will contain "OFF"

Is this possible?

TIA,

Stue.

SELECT Col1,Col2,Col3, Case Col3 When 'A' Then 'ON' Else 'OFF' End From tbl_YourTable

|||

This is definitely possible, use something like the select statement below to create the view:

SELECT Column1, Column2, Column3, Column4 =CASE Column3WHEN'A'THEN'ON'ELSE'OFF'ENDFROM TableA ...
I haven't worked with SQL Server Express so I don't know if CASE is supported. You could alternatively use temp tables etc, but the above statement is probably the least TSQL-intensive. 
|||

Thanks guys!

Just what I needed!

Stue

|||

Chris Pebble:

I haven't worked with SQL Server Express so I don't know if CASE is supported. You could alternatively use temp tables etc, but the above statement is probably the least TSQL-intensive. 

Chris,

As a FYI to you and others that may have the same question... CASE is supported. I am using this code on a Express server.

Thanks again,

Stue

No comments:

Post a Comment