开发者

Swap first name and last names in a column using SQL

开发者 https://www.devze.com 2023-02-16 08:25 出处:网络
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.

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

精彩评论

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

关注公众号