使用另一个表创建表

时间:2014-03-06 03:29:05

标签: sql oracle

我有这个要求。首先,我需要像这样创建emp1表

names
-------------
Hunt, Michael
Hunt, Ethan

通过使用emp1,我需要创建emp2。选择emp2后。输出将是这样的。

FirstLast
-----------
Michael Hunt
Ethan Hunt

4 个答案:

答案 0 :(得分:1)

尝试:

create table emp2 as
select substr(name, instr(name, ', ') + 1, length(name)) as fname,
       substr(name, 1, instr(name, ', ') - 1) as lname
  from emp

SQL小提琴演示:http://sqlfiddle.com/#!4/c4c15/8/0

或单列:

create table emp2 as
select substr(name, instr(name, ', ') + 1, length(name))
       ||' '||substr(name, 1, instr(name, ', ') - 1) as flast
  from emp

答案 1 :(得分:0)

试试此代码

     create table emp1(names nvarchar(50));
    insert into emp1(names) values('Hunt,Michael');
    insert into emp1(names) values('Hunt,Ethan');
    create table emp2(FirstLast nvarchar(50));
    insert into emp2(FirstLast) Select names from emp1;
    select * from emp2;
   select  FirstLast,
   Replace(RIGHT(FirstLast, LEN(FirstLast) - CHARINDEX(',', FirstLast))+' '+ LEFT(FirstLast, CHARINDEX(',', FirstLast)),',','')
        AS UpdatedFirstLast
FROM Emp2

答案 2 :(得分:0)

您可以尝试下面提到的代码:

insert into emp2 
SELECT TRIM(SUBSTR(NAMES,INSTR(NAMES,',',1,1)+1,LENGTH(NAMES) - INSTR(NAMES,',',1,1))||' '||SUBSTR(NAMES,1,INSTR(NAMES,',',1,1)-1)) AS FirstLast FROM EMP1;

答案 3 :(得分:0)

试试此代码

create table emp1(names nvarchar(50));
    insert into emp1(names) values('Hunt,Michael');
    insert into emp1(names) values('Hunt,Ethan');
    select names into emp2 from emp1;
    select  names,
    Replace(RIGHT(names, LEN(names) - CHARINDEX(',', names))+' '+ LEFT(names, CHARINDEX(',', names)),',','')
    AS FirstLast
    FROM emp2