?
Solved

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

Posted on 2011-03-08
3
Medium Priority
?
526 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 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

764 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