soe
soe

Reputation:

How to switch sql-2005 Select Case When T-sql Naming?

select 
(Case Remark01
When 'l1' then type1
when 'l2' then type2
end) AS [?] --Remark .....want to switch name in here
from mytable

Example ....

select 
(Case level
When 'l1' then type1 ('l1' mean check constant string)
when 'l2' then type2(('l2' mean check constant string))
end) AS (Case when 'l1' then [type01] Else [type02])
from mytable
select level,type1,type2 from mytable

I using two program this mytable
one program is want to show menu only type1 only
one program is want to show menu only type2 only
I using one view using two program..

Upvotes: 0

Views: 6643

Answers (2)

Jack Ryan
Jack Ryan

Reputation: 8472

It doesnt make sense to switch the name of a column per row. The rows themselves dont have their own column names, the containing dataset does.

If on the other hand you need to change the name of the column based on data that is input then just selecting the data twice under two names would be easiest. EG:

SELECT  col1 AS alias1,
        col1 AS alias2
FROM    myTable

Upvotes: 1

gbn
gbn

Reputation: 432301

You want to dynamically name the column per row? Unfortunately, this can't be done. However, if you have exactly one row then you could use dynamic SQL EXEC (@sqlstring)

But then how will your client know what column name to expect?

Outside of dynamic SQL or for more than one row, then you could pass the name out as another column...

select
    Case level
      When 'l1' then type1
      when 'l2' then type2
   end AS columnvalue,
    Case level
      When 'l1' then type1
      when 'l2' then type2
   end AS outputcolumnname,
from
   mytable

Upvotes: 1

Related Questions