SQLTeam.com | Weblogs | Forums

Replace

Table having only one column. The column having 5 values those are 'A', 'A','A','B','B'
I want output like this 'B', 'B','B','A','A'.

I got the output by using CASE Statement. Is there any other approach to get the output.

SELECT Column_Name,
CASE WHEN Column_Name = 'A' THEN 'B'
ELSE 'A'
END
FROM EMPDB.DBO.TABLE_ABC;

Possibly..

Replace function

https://docs.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-ver15

I tried below query but i am not getting excepted result. Your help is greatly appropriated.

SELECT REPLACE(REPLACE(Column_Name, 'A', 'B'), 'B', 'A')
FROM EMPDB.DBO.TABLE_ABC

hi i tried to do this please see if this is what you want !!!

first you have to replace A with SomethingElse
then replace B to A and then replace SomethingElse to B

please click arrow to the left for drop create data
drop table #data 
go 

create table #data 
(
Column1 varchar(1)
)
go 

insert into #data select 'A'
insert into #data select 'A'
insert into #data select 'A'
insert into #data select 'B'
insert into #data select 'B'
go 

select * from #data 
go

image

please click arrow to the left for SQL
SELECT  Replace(Replace(Replace(column1, 'A', '5'), 'B', 'A'), '5', 'B') 
FROM #data

Thank you so much # [harishgg1].....I really appreciate your help