Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select statements

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-12 : 23:05:50
Kurt writes "I have a table with columns named:
-keyCol
-indexCol - has a value of 'A', 'B', or 'C'.
-colA
-colB
-colC

Can you get the value of indexCol and use that value to access the correct column out of colA, colB, and colC?

Possibly a subselect which gets indexCol, and then use that to build the string to access the other column?

Thank you for your time."

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-03-12 : 23:24:35
You can use the CASE statement:

SELECT CASE indexCol WHEN 'A' THEN colA WHEN 'B' THEN colB WHEN 'C' THEN colC END
FROM myTable


This will test the indexCol value and substitute the appropriate column values. The thing to keep in mind is that colA, colB and colC must be the same data type, you cannot mix char/varchar with date or numeric datatypes. You can however use the CONVERT() function to convert the different columns to a consistent data type.

Go to Top of Page
   

- Advertisement -