开发者

Reverse order of firstname and lastname in SQL column

I have a column in a SQL database in the format initial space surname, e.g. "J Smith", and I'd like it to be "Smith J". What is the most efficient way of doing this us开发者_StackOverflowing T-SQL? The table has many millions of records. Thanks.


Use the substring formulas, but use them to avoid having to use them ever again:

ALTER TABLE SillyTable ADD FirstInitial char(1), LastName varchar(50);

INSERT INTO SillyTable(FirstInitial, LastName) 
SELECT SUBSTRING(NameColumn, 1, 1), SUBSTRING(NameColumn, 3, LEN(NameColumn) - 2)
FROM SillyTable;

SELECT LastName + ' ' + FirstInitial AS WhatMyBossWantsToday FROM SillyTable;


Assuming the column is indeed 1 character followed by a space followed by the name...

SELECT SUBSTRING(x,3,255)+' '+SUBSTRING(x,1,1)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜