Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

360
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda