你剛剛在最後一個空間分裂?如果是的話這應該工作:
select 'Sam George Jack Peter' as FullName
into #names
union select 'Sam Adam Peter'
select LEFT(FullName,LEN(FullName)-CHARINDEX(' ',REVERSE(FullName))) as FirstName
,RIGHT(FullName,CHARINDEX(' ',REVERSE(FullName))-1) as LastName
from #names
編輯: 爲不帶空格的處理的名稱和把全名作爲姓氏
select 'Sam George Jack Peter' as FullName
into #names
union select 'Sam Adam Peter'
union select 'Peter'
select CASE
WHEN CHARINDEX(' ',FullName) = 0 THEN ''
ELSE LEFT(FullName,LEN(FullName)-CHARINDEX(' ',REVERSE(FullName)))
END as FirstName
,CASE
WHEN CHARINDEX(' ',FullName) = 0 THEN FullName
ELSE LTRIM(RIGHT(FullName,CHARINDEX(' ',REVERSE(FullName))))
END as LastName
from #names
在我的情況我知道它只有一個姓 – Sam 2010-12-10 19:45:27