Data Conversion

Have 2 table Emp1 (Column Name: Ename), 2nd table Emp2 (Column Name: First and Column Name: Last

Table Name :Emp1
EName
Hunt, Michael
Graham, Smith
Nelson, Ryan


Table Name: Emp2
First Last
Michael Hunt
Smith Graham
Ryan Nelson

Need to write a SQL query to convert the data from Emp1 to Emp2 in this way.
Can any body help me.

Thanks

Questions by ssq1999

Showing Answers 1 - 51 of 51 Answers

select SUBSTR('Hunt, Michael',1,INSTR('Hunt, Michael',',')-1) LAST_NAME,SUBSTR('Hunt, Michael',INSTR('Hunt, Michael',',')+1) FIRST_NAME from dual

Here is the example how to use and accordingly change the query and insert into another table.

Thks...

  Was this answer useful?  Yes

sam2233

  • Jul 3rd, 2008
 

 create table emp2 as(select Substr(ename,1,instr(ename,',')-1) as last,
   substr(ename,instr(ename,',')+1,length(ename)) as first  from emp1)

  Was this answer useful?  Yes

akhilbest

  • Oct 20th, 2008
 

insert into emp2(first_name,last_name) (select substr(ename,1,instr(ename,',')-1) ,trim(',' from substr(ename,instr(ename,','))) from emp1 )

  Was this answer useful?  Yes

Vishuraj

  • Jan 9th, 2009
 

Insert into emp2select substring(ename,0,charindex(',',ename)),substring(ename,(charindex(',',ename)+ 1),40) from emp1This will insert into emp2 as First name and Lastname.

  Was this answer useful?  Yes

singh13

  • Feb 2nd, 2009
 

insert into emp2(first,last) (select substr(ename,1,instr(ename,',')-1),substr(ename,instr(ename,',')+1) from emp1)

  Was this answer useful?  Yes

gudluck

  • Jul 16th, 2009
 

SELECT CONCAT(substring_index(Ename,",",-1),substring_index(Ename,",",1)) FROM Emp1;

(Tested in mySQL)

  Was this answer useful?  Yes

vinu1

  • Dec 8th, 2009
 

CREATE TABLE emp2
AS
SELECT substr(ename,instr(ename,',')+1) First,substr(ename,1,instr(ename,',')-1)  Last FROM emp1;

  Was this answer useful?  Yes

Last value in emp1 table "Nelson,Ryan" having the length of 11 different from other values having length of 12.

Not poosible in this case, if it also could be the same length field then it can be resolved using substr() and Trim() functions.

  Was this answer useful?  Yes

sarasmuthu

  • Feb 21st, 2010
 

INSERT INTO emp2 SELECT substr(ename,1,instr(ename,',') -1) as LN , substr(ename,instr(ename,',') +1, length(ename)) as FN FROM emp1;

  Was this answer useful?  Yes

SujitKr

  • Feb 22nd, 2010
 

CREATE TABLE emp2 as SELECT  (SUBSTR(ename,instr(ename,',')+2) ||' ' || SUBSTR(ename,0,instr(ename,',')-1)) ename FROM test

Sujit

  Was this answer useful?  Yes

aryasen.kp

  • Mar 3rd, 2010
 

CREATE TABLE emp2 as SELECT Initcap(trim(substr(Ename,instr(Ename,',')+1,length(Ename)-instr(Ename,',')))) ||' '||Initcap(trim(substr(Ename,1,instr(Ename,',')-1))) firstlast
FROM emp1

  Was this answer useful?  Yes

debjit20

  • Apr 9th, 2010
 

Pls try the following steps:

Create table emp1
(ename varchar2(30))
create table emp2
(first_name varchar2(30), last_name varchar2(30))
insert into emp1 values('Hunt, Michael')
insert into emp1 values('Graham, Smith')
insert into emp1 values('Nelson, Ryan')
select * from emp1
insert into emp2
select trim(substr(ename, instr(ename, ',')+1, length(ename))), trim(substr(ename, 1, instr(ename, ',')-1))
from emp1
select * from emp2

The output will be:

FIRST_NAME   LAST_NAME
Michael              Hunt
Smith                 Graham
Ryan                   Nelson

  Was this answer useful?  Yes

katgeektalk

  • Aug 22nd, 2010
 

SELECT substr(ename instr(ename, ',')+2) First ,substr(ename, 1, instr(ename, ',')-1) Last FROM emp1;
has to be +2 because of the space after comma

  Was this answer useful?  Yes

SQL> select * from name;

NAME
--------------------
Girija,Sankar


  1* create table split_name as(select substr(name,1,(instr(name,',') -1)) "First",substr(name,(inst
SQL> /

Table created.

SQL> select * from split_name;

First                Last
-------------------- --------------------
Girija               Sankar

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions