0
How to 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 didn’t work. How can I do this replace using column values?
If you do so:
SELECT REPLACE(COLUNA_C, 'a', COLUNA_B ) FROM TABELA
works normally. Even I tried to use a cast or Convert in COLUNA_A E COLUNA_B, inside replace, also unsuccessfully.
What does "didn’t work"? How are the columns declared? What is their COLLATE?
– José Diz
@Josédiz Latin1_general_ci_as
– Eduardo Mendes
@Josédiz does not change anything. The query executes, and with replace shows the same thing without replace. The return looks like this: COLUNA_A | COLUMN B | COLUNA_C (replace) A 123 house
– Eduardo Mendes
(1) We need to analyze the individual COLLATE of each column. The database can be in one COLLATE, the instance in another COLLATE and each column in other COLLATE... (2) It is certain that in the column
coluna_A
is there only one character? Beware of spaces and not visible characters.– José Diz
It wasn’t the collate, but it was the invisible spaces and characters. Then just went to do REPLACE(COLUNA_C, LTRIM(RTRIM(COLUNA_A), COLUNA_B) and problem was solved.
– Eduardo Mendes
Bloody spaces... ;)
– José Diz