截取空格前的字符串
钢材 10*10
木地板 10*20
截取钢材/木地板
[解决办法]
declare @str varchar(200)select @str=@str+'/'+left(col,charindex(' ',col)) from testprint right(@str,len(@str)-1)
[解决办法]
--> 测试数据:[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
[解决办法]
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 行受影响)**/
[解决办法]
select right(col,len(col)-charindex(' ',col)) as col from tb
[解决办法]