如何截取第二个“-”前面的字符串

qqlenovo 2011-06-10 11:23:49
如何截取第二个“-”前面的字符串
如 Y-80M1-2/0.75 变成Y-80M1,
Y-80M2-2/1.1 变成Y-80M2


规格型号
Y-80M1-2/0.75
Y-80M2-2/1.1
Y-90S-2/1.5
Y-90L-2/2.2
Y-100L-2/3
Y-112M-2/4
Y-132S1-2/5.5
Y-132S1-2/5.5
Y-132S2-2/7.5
Y-132S2-2/7.5
Y-160M1-2/11
Y-160M1-2/11
Y-160M2-2/15
Y-160M2-2/15
Y-160L-2/18.5
Y-160L-2/18.5
Y-180M-2/22
Y-180M-2/22
Y-200L1-2/30
Y-200L1-2/30
Y-200L2-2/37
Y-200L2-2/37
Y-200L2-2/37
Y-225M-2/45
Y-225M-2/45
Y-225M-2/45
Y-250M-2/55
Y-280S-2/75
Y-280M-2/90
Y-280M-2/90
...全文
489 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
挨踢直男 2011-06-10
  • 打赏
  • 举报
回复
select left(字段,len(字段)-charindex('-',reverse(字段)))
-晴天 2011-06-10
  • 打赏
  • 举报
回复
create table tb(规格型号 nvarchar(20))
insert into tb select 'Y-80M1-2/0.75'
insert into tb select 'Y-80M2-2/1.1'
insert into tb select 'Y-90S-2/1.5'
insert into tb select 'Y-90L-2/2.2'
insert into tb select 'Y-100L-2/3'
insert into tb select 'Y-112M-2/4'
insert into tb select 'Y-132S1-2/5.5'
insert into tb select 'Y-132S1-2/5.5'
insert into tb select 'Y-132S2-2/7.5'
insert into tb select 'Y-132S2-2/7.5'
insert into tb select 'Y-160M1-2/11'
insert into tb select 'Y-160M1-2/11'
insert into tb select 'Y-160M2-2/15'
insert into tb select 'Y-160M2-2/15'
insert into tb select 'Y-160L-2/18.5'
insert into tb select 'Y-160L-2/18.5'
insert into tb select 'Y-180M-2/22'
insert into tb select 'Y-180M-2/22'
insert into tb select 'Y-200L1-2/30'
insert into tb select 'Y-200L1-2/30'
insert into tb select 'Y-200L2-2/37'
insert into tb select 'Y-200L2-2/37'
insert into tb select 'Y-200L2-2/37'
insert into tb select 'Y-225M-2/45'
insert into tb select 'Y-225M-2/45'
insert into tb select 'Y-225M-2/45'
insert into tb select 'Y-250M-2/55'
insert into tb select 'Y-280S-2/75'
insert into tb select 'Y-280M-2/90'
insert into tb select 'Y-280M-2/90'
go
select left(规格型号,charindex('-',规格型号,charindex('-',规格型号)+1)-1) from tb
go
drop table tb
/*
--------------------
Y-80M1
Y-80M2
Y-90S
Y-90L
Y-100L
Y-112M
Y-132S1
Y-132S1
Y-132S2
Y-132S2
Y-160M1
Y-160M1
Y-160M2
Y-160M2
Y-160L
Y-160L
Y-180M
Y-180M
Y-200L1
Y-200L1
Y-200L2
Y-200L2
Y-200L2
Y-225M
Y-225M
Y-225M
Y-250M
Y-280S
Y-280M
Y-280M

(30 行受影响)

*/


jxqn_liu 2011-06-10
  • 打赏
  • 举报
回复
字段 like '_-%'
--小F-- 2011-06-10
  • 打赏
  • 举报
回复
select
case when
charindex('-',Gauge,charindex('-',Gauge)+1)=0
then
Gauge
else
left(Gauge,charindex('-',Gauge,charindex('-',Gauge)+1)-1)
end
from
tb
zml198506 2011-06-10
  • 打赏
  • 举报
回复
可以用正则表达式啊
X_0 2011-06-10
  • 打赏
  • 举报
回复

create table tb(Gauge varchar(20))
insert into tb select 'Y-80M1-2/0.75'
insert into tb select 'Y-80M2-2/1.1'
insert into tb select 'Y-90S-2/1.5'
insert into tb select 'Y-90L-2/2.2'
insert into tb select 'Y-100L-2/3'
insert into tb select 'Y-112M-2/4'
insert into tb select 'Y-132S1-2/5.5'
insert into tb select 'Y-132S1-2/5.5'
insert into tb select 'Y-132S2-2/7.5'
insert into tb select 'Y-132S2-2/7.5'
insert into tb select 'Y-160M1-2/11'
insert into tb select 'B-0.64mm'
insert into tb select 'B-0.80mm'
insert into tb select 'YEJ2-80'
insert into tb select 'B-0.55mm'
insert into tb select 'B-0.41mm'
insert into tb select 'B-0.63mm'
insert into tb select 'M8*20'
insert into tb select 'M16*160'


select Case When charindex('-',Gauge,charindex('-',Gauge)+1)=0 then Gauge
Else left(Gauge,charindex('-',Gauge,charindex('-',Gauge)+1)-1)
end
from tb


qqlenovo 2011-06-10
  • 打赏
  • 举报
回复
增加一些规格型号:

B-0.64mm
B-0.80mm
YEJ2-80
B-0.55mm
B-0.41mm
B-0.63mm
M8*20
M16*160
也就是有些型号只有1个"-",有些没有"-"

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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