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 am using two programs this mytable
one program wants to show only menu type1 only
one program wants to show only menus of type2

I am using one view using two programs.

0


source to share


2 answers


Do you want to name the column dynamically for each row? Unfortunately this is not possible. However, if you have exactly one row, you can use dynamic SQLEXEC (@sqlstring)

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



Outside of dynamic SQL or more than one row, you can pass the name 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

      

+1


source


It doesn't make sense to switch the column name to string. The rows themselves do not have their own column names that contain the dataset.

If, on the other hand, you need to change the column name based on the entered data, simply selecting the data twice under two names will be the easiest. EG:



SELECT  col1 AS alias1,
        col1 AS alias2
FROM    myTable

      

+1


source







All Articles