日期:2014-05-18  浏览次数:20407 次

字符串处理的问题
CCCIL-云南-法务-法务
CCCIL-云南-需求满足-基建工程
CCCIL-云南-总经办-总经办
CCCIL-云南-法务

以上的字符串,怎样准确的得到:

CCCIL-云南-法务
CCCIL-云南-需求满足
CCCIL-云南-总经办
CCCIL-云南-法务
这样的数据

也就是包括第三个'-'符号以后的数据都不要了

------解决方案--------------------
SQL code
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] varchar(28))
insert [tb]
select 'CCCIL-云南-法务-法务' union all
select 'CCCIL-云南-需求满足-基建工程' union all
select 'CCCIL-云南-总经办-总经办' union all
select 'CCCIL-云南-法务'
 
---查询---
select
  col=left(col,len(col)-charindex('-',reverse(col)))
from 
  tb

---结果---
col
----------------------------
CCCIL-云南-法务
CCCIL-云南-需求满足
CCCIL-云南-总经办
CCCIL-云南

(4 行受影响)

------解决方案--------------------