34,588
社区成员
发帖
与我相关
我的任务
分享
--接1楼
declare @str varchar(200), @str2 varchar(200)
select
@str=isnull(@str+'/','') + left(col,charindex(' ',col)),
@str2=isnull(@str+'/','') + right(col,charindex(' ',reverse(col))-1)
from test
print @str, @str2
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 行受影响)
**/
--> 测试数据:[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)
/*
钢材/木地板
*/
declare @str varchar(200)
select @str=@str+'/'+left(col,charindex(' ',col)) from test
print right(@str,len(@str)-1)
select RIGHT(col,charindex(' ',REVERSE(col))-1) as col from tb
/**
col
--------------------
10*10
10*20
(2 行受影响)
**/
select right(col,len(col)-charindex(' ',col)) as col from tb