Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
¿Cómo escribir consultas con funciones de conversión y cambio en SQL Server?

Tengo que convertir una columna int en una columna de texto y reemplazar los valores enteros dentro de esa columna.

Por ejemplo, tengo un status de columna que puede contener valores como 0, 1, 2, 3, 4, 5, 6, 7.

Para '0' tengo que reemplazar el valor con "New" , para '1' con "Identified" y así sucesivamente.

Para este escenario, ¿cómo escribir una consulta SQL?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Personalmente, iría con una tabla de mapeo, pero otra opción es CHOOSE() o incluso el CASE tradicional

Tenga en cuenta el +1 en la opción CHOOSE ... 0 no es una opción válida y devolvería NULL

Ejemplo

 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

Resultados

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

0

Podría crear una tabla (temporal) con esa asignación.

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

Inserte todos los valores y luego únalos.

over 4 years ago · Santiago Trujillo Report

0

Pasos a seguir para convertir la columna int a texto y reemplazar los valores existentes.

  1. Alterar la mesa. Dado que INT se convierte en texto implícitamente, NOT NULL se puede mantener
  2. Instrucción EXEC UPDATE usando la tabla de conversión especificada usando el constructor de valores de la tabla VALUES

Algo como esto

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!