请教 SQL Server2005中截取字符串问题?
有一个字段内容如下:
ASB-34A1-P033
AB-S2-D3
ABC-S32-D34
KJ-K4-ER-JK
H5-G323-FSS-G54
...
要求得到如下第二个'-'符号前面的数字如下:
1
2
32
4
323
------解决方案--------------------
- SQL code
--写的太无聊了create table tb(name varchar(20))insert into tb select 'ASB-34A1-P033' unionselect 'AB-S2-D3' unionselect 'ABC-S32-D34' unionselect 'H5-G323-FSS-G54' select REVERSE(left(REVERSE(left(RIGHT(name,len(name)-CHARINDEX('-',name)), CHARINDEX('-',RIGHT(name,len(name)-CHARINDEX('-',name)))-1)), PATINDEX('%[^0-9]%',REVERSE(left(RIGHT(name,len(name)-CHARINDEX('-',name)), CHARINDEX('-',RIGHT(name,len(name)-CHARINDEX('-',name)))-1)))-1))from tb/*--------------------3221323
------解决方案--------------------
- SQL code
select substring(stuff(name,1,charindex('-',name),''),charindex('-',stuff(name,1,charindex('-',name),''))-1,1)from tbwhere len(name)-len(replace(name,'-','')) >= 2
------解决方案--------------------