截取空格前的字符串

bruceoffice 2012-04-28 11:48:33
钢材 10*10
木地板 10*20

截取钢材/木地板
...全文
391 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
simonxt 2012-04-29
  • 打赏
  • 举报
回复
--接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
晓风斜阳 2012-04-29
  • 打赏
  • 举报
回复
思路:
SELECT SUBSTRING('钢材 10*10',1,CHARINDEX(' ','钢材 10*10'))
ahidden 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

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)……
[/Quote]
学习
百年树人 2012-04-28
  • 打赏
  • 举报
回复
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 行受影响)
**/
暖暖2021 2012-04-28
  • 打赏
  • 举报
回复
select left('钢材 10*10',CHARINDEX(' ','钢材 10*10')-1) as a
暖暖2021 2012-04-28
  • 打赏
  • 举报
回复
select left('钢材 10*10',CHARINDEX(' ','钢材 10*10')) as a
  • 打赏
  • 举报
回复

--> 测试数据:[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)
百年树人 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
钢材 10*10
木地板 10*20

截取右边的呢
10*20
[/Quote]

select RIGHT(col,charindex(' ',REVERSE(col))-1) as col from tb

/**
col
--------------------
10*10
10*20

(2 行受影响)
**/
十三门徒 2012-04-28
  • 打赏
  • 举报
回复

select right(col,len(col)-charindex(' ',col)) as col from tb
bruceoffice 2012-04-28
  • 打赏
  • 举报
回复
钢材 10*10
木地板 10*20

截取右边的呢
10*20

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧