字符串的动态截取

wea1978 2006-04-26 04:09:35
有这么些个字符串,要取其中的一部份:
CM6W-15M±0.1%T
CMD1/2W-20K±5%4
FD1/4W-1K±1%36M
FD3W-1M±10%1

以上是举例,规律就是前面2个或3个字母后面都是数字,比如6w,1/2W之类,
在后都有个%号
需要找出其中的字符:
6W,±0.1%
1/2W,±5%
1/4W,±1%
3W,±10%
...全文
339 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
hglhyy 2006-04-27
  • 打赏
  • 举报
回复
好了就接分!
wea1978 2006-04-27
  • 打赏
  • 举报
回复
谢谢!已解决!
子陌红尘 2006-04-27
  • 打赏
  • 举报
回复
declare @t table(str varchar(20))
insert into @t select 'CM6W-15M±0.1%T'
insert into @t select 'CMD1/2W-20K±5%4'
insert into @t select 'FD1/4W-1K±1%36M'
insert into @t select 'FD3W-1M±10%1'

select
substring(str,charindex('-',str)+1,charindex('±',str)-charindex('-',str)-1) as str
from @t

/*
str
-----------------------------------------
15M
20K
1K
1M
*/
wea1978 2006-04-26
  • 打赏
  • 举报
回复
用libin_ftsafe(子陌红尘)的方案可以解决问题
?号的问题使用replace替换掉就ok了...
如果要取'-'号后'±'前的字符也是用同样的方法吗?
还是要测试一下...谢谢!
子陌红尘 2006-04-26
  • 打赏
  • 举报
回复
或者:

select ... from 表 where charindex('±',字段)>0
子陌红尘 2006-04-26
  • 打赏
  • 举报
回复
select ... from 表 where charindex('?',字段)=0
xeqtr1982 2006-04-26
  • 打赏
  • 举报
回复
那这个字段中其他的记录中会不会有带问号的记录?
wea1978 2006-04-26
  • 打赏
  • 举报
回复
其它都一样,就是±成了'?'号
xeqtr1982 2006-04-26
  • 打赏
  • 举报
回复
那两条记录什么样子?
wea1978 2006-04-26
  • 打赏
  • 举报
回复
手动改过来是不怎么现实的...因为关系到责任的问题,除非是就那样放在那里,软件就不识别那两个,
xeqtr1982 2006-04-26
  • 打赏
  • 举报
回复
手动改过来可以吗?
wea1978 2006-04-26
  • 打赏
  • 举报
回复
不行,因为正常的情况是±,就偶尔有几个成了'?',我刚查了下,6万多条数,只有2条是'?',应该改数据库吗?
xeqtr1982 2006-04-26
  • 打赏
  • 举报
回复
把?替换成±呢,行吗

?
wea1978 2006-04-26
  • 打赏
  • 举报
回复
并且这个'?'号还真不好过滤呢,如果用 like '?%',这样是行不通的...
请指教!谢谢!
wea1978 2006-04-26
  • 打赏
  • 举报
回复
如果那±号被人弄成了'?'号怎么办?,是不是加多一个'?'号过滤呢?
wea1978 2006-04-26
  • 打赏
  • 举报
回复
感谢楼上几位,测试ing...
子陌红尘 2006-04-26
  • 打赏
  • 举报
回复
declare @t table(str varchar(20))
insert into @t select 'CM6W-15M±0.1%T'
insert into @t select 'CMD1/2W-20K±5%4'
insert into @t select 'FD1/4W-1K±1%36M'
insert into @t select 'FD3W-1M±10%1'

select
substring(str,case isnumeric(substring(str,3,1)) when 1 then 3 else 4 end,charindex('W',stuff(str,1,3,''))+3-case isnumeric(substring(str,3,1)) when 1 then 2 else 3 end)+','
+
substring(str,charindex('±',str),charindex('%',str)-charindex('±',str)+1)
as str
from @t

/*
str
-----------------------------------------
6W,±0.1%
1/2W,±5%
1/4W,±1%
3W,±10%
*/
子陌红尘 2006-04-26
  • 打赏
  • 举报
回复
看错了,更正一下:
-------------------------------------------------------------------------------------

declare @t table(str varchar(20))
insert into @t select 'CM6W-15M±0.1%T'
insert into @t select 'CMD1/2W-20K±5%4'
insert into @t select 'FD1/4W-1K±1%36M'
insert into @t select 'FD3W-1M±10%1'

select
substring(str,case isnumeric(substring(str,3,1)) when 1 then 3 else 4 end,charindex('W',str)-case isnumeric(substring(str,3,1)) when 1 then 2 else 3 end)+','
+
substring(str,charindex('±',str),charindex('%',str)-charindex('±',str)+1)
as str
from @t

/*
str
-----------------------------------------
6W,±0.1%
1/2W,±5%
1/4W,±1%
3W,±10%
*/
xeqtr1982 2006-04-26
  • 打赏
  • 举报
回复
--这样?
declare @t table([str] varchar(20))
insert into @t select 'CM6W-15M±0.1%T'
union all select 'CMD1/2W-20K±5%4'
union all select 'FD1/4W-1K±1%36M'
union all select 'FD3W-1M±10%1'
union all select 'TE3W-31M±90%1'

select substring([str],patindex('%[0-9]%',[str]),charindex('-',[str])-patindex('%[0-9]%',[str]))+','+substring([str],charindex('±',[str]),charindex('%',[str])-charindex('±',[str])+1) from @t
rivery 2006-04-26
  • 打赏
  • 举报
回复
--检查第三个是否字母是就取出到w为结尾的作为第一个字符串。第二个字符串取±之间部分%
--如果上面的定义就是楼主的意思。那么可以如下操作。
declare @t table(a varchar(20))
insert into @t
select 'CM6W-15M±0.1%T' union
select 'CMD1/2W-20K±5%4' union
select 'FD1/4W-1K±1%36M' union
select 'FD3W-1M±10%1'

select case isnumeric(substring(a,3,1)) when 1 then substring(a,3,charindex('w',a)-2)
else substring(a,4,charindex('w',a)-3) end,
substring(a,charindex('±',a),charindex('%',a)-charindex('±',a)+1)
from @t
/*
6W ±0.1%
1/2W ±5%
1/4W ±1%
3W ±10%

*/
加载更多回复(1)

34,591

社区成员

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

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