如何从同一个表名中的值更新表列名?

时间:2012-07-04 12:46:24

标签: sql sql-server-2008

  eid   |        ename          |    fname       midname  |   lname
--------+-----------------------+-------------+-----------+---------- 
   1            hansen
   2          hansen ola
   3       dennis richard hog

现在如何将它们更新为:

  eid   |        ename          |    fname       midname  |   lname
--------+-----------------------+-------------+-----------+---------- 
   1             hansen
   2           hansen ola            hansen        ola
   3       dennis richsard hog       dennis      richard       hog

3 个答案:

答案 0 :(得分:0)

只需从表格中获取ename,然后使用explode(' ', $name)str_word_count ($name, 1)将其分解为名字,中间名和姓氏,并相应地更新表格。

用于选择行的SQL:

SELECT * FROM <tablename>

用于更新行的SQL:

UPDATE <tablename> SET <columnname> = <value> WHERE ename = <ename>

答案 1 :(得分:0)

如果名称始终采用相同的顺序,您只需使用str_word_count str_word_count ($ result, 1)检索数组中字符串的单词。

它可以帮助您或您希望我发展吗?

答案 2 :(得分:0)

您好请查看下面的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