首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

截取空格前的字符串解决思路

2012-05-04 
截取空格前的字符串钢材 10*10木地板 10*20截取钢材/木地板[解决办法]SQL codedeclare @str varchar(200)s

截取空格前的字符串
钢材 10*10
木地板 10*20

截取钢材/木地板

[解决办法]

SQL code
declare @str varchar(200)select @str=@str+'/'+left(col,charindex(' ',col)) from testprint right(@str,len(@str)-1)
[解决办法]
SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([col] varchar(20))insert [test]select '钢材 10*10' union allselect '木地板 10*20'godeclare @str varchar(200)set @str=''select @str=@str+'/'+left(col,charindex(' ',col)-1) from testprint right(@str,len(@str)-1)/*钢材/木地板*/
[解决办法]
select left('钢材 10*10',CHARINDEX(' ','钢材 10*10')) as a
[解决办法]
select left('钢材 10*10',CHARINDEX(' ','钢材 10*10')-1) as a
[解决办法]
SQL code
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([col] varchar(20))insert [tb]select '钢材 10*10' union allselect '木地板 10*20'goselect left(col,charindex(' ',col)-1) as col from tb/**col--------------------钢材木地板(2 行受影响)**/
[解决办法]
探讨

SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] varchar(20))
insert [tb]
select '钢材 10*10' union all
select '木地板 10*20'
go

select left(col,charindex(' ',col)-1)……

[解决办法]

SQL code
select right(col,len(col)-charindex(' ',col)) as col from tb
[解决办法]
探讨
钢材 10*10
木地板 10*20

截取右边的呢
10*20

热点排行