SQLTeam.com | Weblogs | Forums

Query Column alias name change

Hi

I have a my query ; (not table)

Select
Name ,
Surname
From List

Lang table

Lang Other Explain OtherExplain
En Tr Name A
En Tr Surname B
En Ac Name C
En Ac Surname D

I want to Column name alias from Other Explain if lang Tr then
select
Name as A,
Surname as B
From List

How I do it?
Thanks.

hi

put it in quotes like this

column123 AS " what is the name of the dog"

Hi

I want to make aliases dynamic. For example, if I want Tr (select Name as A ), if I want EN (Select Name as C), it should come. I want to create a new query by bringing the value I am looking for from the other table as alias.
Thanks..

this looks easy to do

but please provide Sample Data with create table insert statements

1 Like

Hi

create table #list(Name varchar(100),Surname varchar(100))
insert into #list select 'Jack','Brown'
insert into #list select 'Tom','Cruise'
insert into #list select 'Oh','God'
select * from #list
drop table #list

create table #lang(lang varchar(100),otherlang varchar(100),explain varchar(100),other_explain varchar(100))
insert into #lang select 'En','tr','Name','NameA'
insert into #lang select 'En','tr','Surname','SurnameA'
insert into #lang select 'En','ae','Name','NameB'
insert into #lang select 'En','ae','Surname','SurnameB'
select * from #lang
drop table #lang

If I select Tr Create query response ;

select Name as NameA, Surname as SurnameA from #List

If I select ae Create query response ;

select Name as NameB, Surname as SurnameB from #List

create table #list(Name varchar(100),Surname varchar(100))
insert into #list select 'Jack','Brown'
insert into #list select 'Tom','Cruise'
insert into #list select 'Oh','God'
select * from #list


create table #lang(lang varchar(100),otherlang varchar(100),explain varchar(100),other_explain varchar(100))
insert into #lang select 'En','tr','Name','NameA'
insert into #lang select 'En','tr','Surname','SurnameA'
insert into #lang select 'En','ae','Name','NameB'
insert into #lang select 'En','ae','Surname','SurnameB'
select * from #lang

-----------------------------------------------------------
declare @otherlang varchar(10) = ''
------------------------------------------------------------
set  @otherlang = 'tr' 
select @otherlang

-- if we change  to set @otherlang = 'ae' then NameB SurnameB comes 

if @otherlang = 'tr' 
select name as 'NameA',surname as 'SurNameA' from #list 

if @otherlang = 'ae' 
select name as 'NameB',surname as 'SurNameB' from #list 


drop table #List 
drop table #Lang

image

1 Like