Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing existing column with values based on condition from other values SQL Server

I want to replace values from my table with values from query result. The query looks like this:

select *,
    case
        when car_model = 'Altima' THEN 'Nissan'
        when car_model = 'F-150' THEN 'Ford'
        when car_model = 'Civic' THEN 'Honda'
        when car_model = 'Silverado' THEN 'Chevrolet'
    END
FROM car_sales_data
WHERE car_model IN ('Altima','F-150','Civic','Silverado')

Basically, this is data cleaning task to do. I am working on it for few hours now and I can’t come to any working solution…

**Important thing is that I want these new values to be saved in a table so I can do some analysis afterwards.
**

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried different things like INSERT INTO, creating table, none of it worked for me. I am a beginner 🙂

>Solution :

update car_sales_data
set car_model=
case
        when car_model = 'Altima' THEN 'Nissan'
        when car_model = 'F-150' THEN 'Ford'
        when car_model = 'Civic' THEN 'Honda'
        when car_model = 'Silverado' THEN 'Chevrolet'
        else car_model
    END

WHERE car_model IN ('Altima','F-150','Civic','Silverado')

Base Data

   create table car_sales_data(car_model varchar(100))
insert into car_sales_data values('Altima')
insert into car_sales_data values('F-150')
insert into car_sales_data values('Civic')
insert into car_sales_data values('Civic')

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading