Solved

TSQL - Nested a case statment to insert values into a table

Posted on 2011-03-08
3
518 Views
Last Modified: 2012-06-21
I'm working on an insert / select statement for migrating data from one database to another.

The host DB has a flat table structure so I need to select a field which could be A, B or C for example and somehow convert this value so that A = 1, B = 2 and C= 3 in the destination table.

My script uses an insert and selecr statement to import the data? Being a newcomer to TSQL i', not sure what the best way to handle this would be? e.g Case Statement imbedded in the select clause?
0
Comment
Question by:mbs2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35068108
you have 2 options, roughly:
hard-code the "translation":
select case when FIELD = 'A' THEN 1 WHEN field = 'B' THEN 2 ... etc END  

Open in new window


or create a mapping table with the translations:
 create table tblMapping1 (field_value varchar(100), value_replacement varchar(100));
insert into tblMapping1 values ('A', '1');
insert into tblMapping1 values ('B', '2');
etc 

Open in new window


and use that table:
select t.field, m.value_replacement
  from yourtable t
  left join tblMapping1 m
   on m.field_value = t.field

Open in new window


hope this helps
0
 
LVL 18

Expert Comment

by:deighton
ID: 35068122
SELECT ShipVia, CASE ShipVia
    WHEN 1 THEN 'A.Datum'
    WHEN 2 THEN 'Contoso'
    WHEN 3 THEN 'Consolidated Messenger'
        ELSE 'Unknown'
        END AS MT FIELD
FROM Orders
0
 
LVL 6

Expert Comment

by:jonaska
ID: 35068367
INSERT INTO myImportTable (myDestCol)
SELECT
CASE MySrcCol
 WHEN 'A' THEN 1
 WHEN 'B' THEN 2
 WHEN 'C' THEN 3
END AS MySrcCol

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question