您好 检查下面的 SQL 代码以获取更新语句。
CREATE TABLE emptble(eid int,ename varchar(200) NULL,fname varchar(50) NULL,midname varchar(25) NULL,lname varchar(100) NULL)
INSERT INTO emptble(eid,ename)
VALUES(1,'hansen'),(2,'hansen ola'),(3,'dennis richard hog')
SELECT ename,
CASE WHEN CHARINDEX(' ',ename,1)>0 then LEFT(ename,CHARINDEX(' ',ename,1)-1) ELSE ename END,
CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)>0 THEN LEFT((CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)-1) ELSE (CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END) END,
CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1) = 0 THEN NULL ELSE RIGHT(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END,LEN(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END)-CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)) END
FROM emptble
UPDATE emptble SET fname= CASE WHEN CHARINDEX(' ',ename,1)>0 then LEFT(ename,CHARINDEX(' ',ename,1)-1) ELSE ename END,
midname =CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)>0 THEN LEFT((CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)-1) ELSE (CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END) END,
lname=CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1) = 0 THEN NULL ELSE RIGHT(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END,LEN(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END)-CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)) END
SELECT * FROM emptble