Replace SQL SERVER between columns

0

How to do a replace between columns in SQL SERVER. For example, given the table below:

COLUNA_A | COLUNA B | COLUNA_C
A          123        casa

I tried to run the query:

SELECT REPLACE(COLUNA_C, COLUNA_A, COLUNA_B ) FROM TABELA

To try to get the result:

COLUNA_A | COLUNA B | COLUNA_C
A          123        c123s123

But it did not work. How can I make this replace using the values of the columns?

If you do this:

SELECT REPLACE(COLUNA_C, 'a', COLUNA_B ) FROM TABELA

works normally. Even I tried to cast or convert to COLUMA_A and COLUNA_B, inside the replace, also without success.

    
asked by anonymous 09.12.2018 / 14:24

1 answer

1

The construction with the REPLACE () function is correct. If it did not work, one hypothesis is that the columns are set to case sensitive . If this is the case, A is different from a .

To evaluate whether the reason is case sensitive grouping, try:

-- código #1 v2
IF Object_ID('TABELA','U') is not null
  DROP TABLE dbo.TABELA;
go

CREATE TABLE dbo.TABELA (
     coluna_A varchar(20) collate Latin1_General_CS_AS, 
     coluna_B varchar(20) collate Latin1_General_CS_AS, 
     coluna_C varchar(20) collate Latin1_General_CS_AS
);

INSERT into dbo.TABELA (coluna_A, coluna_B, coluna_C) values
  ('A', '123', 'casa_X'),
  ('a', '123', 'casa_Y');

SELECT replace (coluna_C, coluna_A, coluna_B)
  from dbo.TABELA;

SELECT replace (coluna_C collate Latin1_General_CI_AS, coluna_A, coluna_B)
  from dbo.TABELA;

I did not test code # 1 after the modification. May contain error (s).

    
09.12.2018 / 15:37