日期:2014-05-17  浏览次数:20367 次

关于空格的问题
我想查出第二个空格前的内容:
   例如:
序号      性别       名字
1          男        LeBron Raymone James
2          男        Dwyane Wade James Jack
3          女        LeBron Raymone Jack
SQL:
select name from table1
我先要它出来的数据为:
LeBron Raymone
Dwyane Wade
LeBron Raymone

请问如何实现?谢谢
------解决方案--------------------
LeBron James

Dwyane Wade 两大巨星啊
------解决方案--------------------
REVERSE(PARSENAME (replace(REVERSE (名字),' ','.'),1))+' '+ REVERSE(PARSENAME (replace(REVERSE (名字),' ','.'),2))

or 
xml
------解决方案--------------------
引用:
LeBron James

Dwyane Wade 两大巨星啊


请回答正题
------解决方案--------------------
----------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2011-04-25 16:27:38
-- Verstion:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
-- Jul  9 2008 14:43:34 
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([序号] int,[性别] varchar(2),[名字] varchar(60))
insert [tb]
select 1,'男','LeBron Raymone James' union all
select 2,'男','Dwyane Wade James Jack' union all
select 3,'女','LeBron Raymone Jack' 
--------------开始查询--------------------------
select
   序号,
   性别,
   reverse(PARSENAME(replace(reverse(名字),' ','.'),1))+' '+reverse(PARSENAME(replace(reverse(名字),' ','.'),2))
from
   tb
----------------结果----------------------------
/* 序号          性别   
----------- ---- ----------------------------------------------------------------------------------------------------------------
1           男    LeBron Raymone
2           男    Dwyane Wade
3           女    LeBron Raymone

*/

------解决方案--------------------
最多只能3个空格 多了就要用函数
------解决方案--------------------