Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

363
Visualizações
How to write query with cast and switch functions in SQL Server?

I have to convert an int column to a text column and replace the integer values within that column.

For example I have a column status that can contain values like 0, 1, 2, 3, 4, 5, 6, 7.

For '0' I have to replace the value with "New", for '1' with "Identified" and so on.

For this scenario how to write a SQL query?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Personally, I'd go with a mapping table, but another option is CHOOSE() or even the traditional CASE

Note the +1 in the CHOOSE option ... 0 is not a valid option and would return NULL

Example

Declare @YourTable Table ([Status] int)
Insert Into @YourTable Values 
 (0),(1),(2),(3)

 
Select * 
      ,ViaCHOOSE = choose([Status]+1,'New','Identified','Some Other','...')
      ,ViaCASE   = case [Status] 
                        when 0 then 'New'
                        when 1 then 'Identified'
                        when 2 then 'Some Other'
                        else null  -- or 'Undefined'
                    end
From @YourTable

Results

Status  ViaCHOOSE   ViaCASE
0       New         New
1       Identified  Identified
2       Some Other  Some Other
3       ...         ...
over 4 years ago · Santiago Trujillo Relatório

0

You could create a (temporary) table with that mapping.

create table XYMapping (number int, text varchar(max));
INSERT INTO XYMapping (number, text) VALUES (1, 'New'), (2, 'Identified'); -- ...

Insert all values and then join them.

over 4 years ago · Santiago Trujillo Relatório

0

Steps to follow to convert int column to text and replace the existing values.

  1. Alter the table. Since INT converts to text implicitly NOT NULL is able to be maintained
  2. Exec UPDATE statement using conversion table specified using VALUES table value constructor

Something like this

drop table if exists #samples;
go
create table #samples (
  id           int not null,
  stat         varchar(10) not null);

insert #samples(id, stat) values 
(10, 'Start'),
(1, 'Start'),
(1, 'Failed'),
(2, 'Start'),
(3, 'Start'),
(3, 'Failed'),
(4, 'Start'),
(4, 'Failed'),
(4, 'Start'),
(4, 'Failed');

/* step 1: alter the table (and implicitly convert) */
alter table #samples
alter column
  id        varchar(20) not null;

/* step 2: update based on conversion table */
update s
    set id=v.new_str
from #samples s
     join 
     (values ('1', 'New'),
             ('2', 'Identified'),
             ('3', 'Identified'),
             ('4', 'Identified'),
             ('10', 'Other')) v(old_int, new_str) on s.id=v.old_int;

select * from #samples;
id          stat
Other       Start
New         Start
New         Failed
Identified  Start
Identified  Start
Identified  Failed
Identified  Start
Identified  Failed
Identified  Start
Identified  Failed
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda