开发者

Swap first name and last names in a column using SQL

How do I change the first name and last name from a given name, for example: I have a name "Krishna Kiran" with Krishna as the first name and Kiran as the last name (surname) in a column.

Now I need the output as "Kiran, Krishna", that is, lastname, firstname. How c开发者_JS百科an I do this?


SELECT ISNULL(LastName + ', ', '') 
       + ISNULL(FirstName, '') AS FormattedName FROM ...


declare @T table (Name varchar(50))

insert into @T values ('krishna kiran')

select
  right(Name, len(Name)-charindex(' ', Name, 0))+', '+left(Name, charindex(' ', Name, 0))
from @T


Try This


Try This

UPDATE TableName set first_name=(@temp:=first_name),first_name=last_name,last_name=@temp;

refer the image : https://i.stack.imgur.com/ri2AU.jpg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜