日期:2014-05-18  浏览次数:20466 次

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

截取钢材/木地板

------解决方案--------------------
SQL code

declare @str varchar(200)
select @str=@str+'/'+left(col,charindex(' ',col)) from test
print 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 all
select '木地板 10*20'
go
declare @str varchar(200)
set @str=''
select @str=@str+'/'+left(col,charindex(' ',col)-1) from test
print 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]
go
create table [tb]([col] varchar(20))
insert [tb]
select '钢材 10*10' union all
select '木地板 10*20'
go

select 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