关于空格的问题
我想查出第二个空格前的内容:
例如:
序号 性别 名字
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
[解决办法]
请回答正题
[解决办法]
----------------------------
-- 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个空格 多了就要用函数
------解决方案--------------------
为什么有的查出来是正确的有的是null
[解决办法]
如果超过3个空格怎么写 谢谢
[解决办法]
create table table1(序号 int,性别 varchar(10), 名字 varchar(100))
insert table1
select 1 ,'男', 'LeBron Raymone James' union
select 2 ,'男', 'Dwyane Wade James Jack' union
select 3 ,'女', 'LeBron Raymone Jack'
select rtrim(left(名字,charindex(' ',名字,charindex(' ',名字)+1))) from table1
/*
------------------------------------
LeBron Raymone
Dwyane Wade
LeBron Raymone
(所影响的行数为 3 行)
*/