如何在复制表时转换列的值

时间:2012-02-29 04:17:07

标签: sql sql-server-2008

我有Table1和Table2,两者都有列Name,Lastname,Language。 我必须从Table1导入到表2,只有在Table1中,Language的值是'E','S','F',当我将它导入Table2时,它必须转换为'English','Spanish, '法国'。

你能给我一个SQL语句吗?

(MS SQL 2008)

2 个答案:

答案 0 :(得分:1)

尝试一下:

insert into table2 
  select name, lastname,
    case language
      when 'E' then 'English'
      when 'S' then 'Spanish'
      when 'F' then 'French'
    else
      'Unknown'
    end
  from table1

答案 1 :(得分:0)

INSERT INTO table2 (name, lastname, language)
SELECT t1.name, t1.lastname, 
CASE 
WHEN t1.language = 'E' THEN 'English'
WHEN t1.language = 'S' THEN 'Spanish'
WHEN t1.language = 'F' THEN 'French'
END
FROM table1 t1
WHERE (t1.language = 'E' OR t1.language = 'S' OR t1.language = 'F')