我有這個要求。首先,我需要像這樣創建emp1表使用另一個表格創建表格
names
-------------
Hunt, Michael
Hunt, Ethan
通過使用emp1,我需要創建emp2。選擇emp2後,輸出將如此。
FirstLast
-----------
Michael Hunt
Ethan Hunt
我有這個要求。首先,我需要像這樣創建emp1表使用另一個表格創建表格
names
-------------
Hunt, Michael
Hunt, Ethan
通過使用emp1,我需要創建emp2。選擇emp2後,輸出將如此。
FirstLast
-----------
Michael Hunt
Ethan Hunt
嘗試:
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
試試這個代碼
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
你可以試試下面提及代碼:
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;
使用emp1我需要創建emp2先生請你指導thanx提前 – viki
試試這個代碼
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
是*每*記錄在此*確切*格式? – Brad
是的確切格式先生 – viki