当前位置: 代码迷 >> Sql Server >> 截取空格前的字符串解决思路
  详细解决方案

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

热度:49   发布时间:2016-04-27 13:02:38.0
截取空格前的字符串
钢材 10*10
木地板 10*20

截取钢材/木地板

------解决方案--------------------
SQL code
declare @str varchar(200)select @[email protected]+'/'+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 @[email protected]+'/'+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
  相关解决方案