27,579
社区成员
发帖
与我相关
我的任务
分享
create table tb(SName nvarchar(100))
insert into tb
select 'YellowPageList/中国食品有限公司/供应链部/酒业工厂管理中心/烟台工厂/财务部/成本管理科' union all
select 'YellowPageList/中国食品有限公司/供应链部/酒业工厂管理中心/君顶酒庄/财务部' union all
select 'YellowPageList/中国食品有限公司/供应链部/方便食品工厂管理中心/北京厂/财务部' union all
select 'YellowPageList/中国食品有限公司/供应链部/酒业工厂管理中心/沙城工厂/财务部' union all
select 'YellowPageList/中国食品有限公司/北部管理中心/财务部' union all
select 'YellowPageList/中国食品有限公司/财务部/销售财务支持中心' union all
select 'YellowPageList/中国食品有限公司/财务部/会计管理部' union all
select 'YellowPageList/中国食品有限公司/财务部/运营分析中心' union all
select 'YellowPageList/中粮置地有限公司/酒店事业部/财务部' union all
select 'YellowPageList/中粮粮油有限公司/财务部' union all
select 'YellowPageList/中国食品有限公司/北部管理中心/东北销售大区/大区财务部' union all
select 'YellowPageList/中粮营养健康研究院/财务部' union all
select 'YellowPageList/中粮置地有限公司/中粮地产(集团)股份有限公司/财务部' union all
select 'YellowPageList/中粮屯河股份有限公司/财务部/资金管理部' union all
select 'YellowPageList/中粮肉食投资有限公司/财务部' union all
select 'YellowPageList/中国食品有限公司/东部管理中心/财务部' union all
select 'YellowPageList/中国食品有限公司/东部管理中心/财务部' union all
select 'YellowPageList/中国食品有限公司/南部管理中心/华南销售大区/大区财务部' union all
select 'YellowPageList/中粮粮油有限公司/粮贸部/财务审计部' union all
select 'YellowPageList/中国土产畜产进出口总公司/中土畜财务部' union all
select 'YellowPageList/中国粮油控股有限公司/生化能源事业部/财务部' union all
select 'YellowPageList/中国粮油控股有限公司/生化能源事业部/财务部' union all
select 'YellowPageList/中国粮油控股有限公司/生化能源事业部/财务部' union all
select 'YellowPageList/中国粮油控股有限公司/生化能源事业部/财务部' union all
select 'YellowPageList/中国粮油控股有限公司/小麦加工事业部/财务部'
go
select left(sname,charindex('/',sname,charindex('/',sname)+1)-1) from tb
/*
----------------------------------------------------------------------------------------------------
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中粮置地有限公司
YellowPageList/中粮粮油有限公司
YellowPageList/中国食品有限公司
YellowPageList/中粮营养健康研究院
YellowPageList/中粮置地有限公司
YellowPageList/中粮屯河股份有限公司
YellowPageList/中粮肉食投资有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中国食品有限公司
YellowPageList/中粮粮油有限公司
YellowPageList/中国土产畜产进出口总公司
YellowPageList/中国粮油控股有限公司
YellowPageList/中国粮油控股有限公司
YellowPageList/中国粮油控股有限公司
YellowPageList/中国粮油控股有限公司
YellowPageList/中国粮油控股有限公司
(25 行受影响)
*/
go
drop table tb
如果要防止字段中可能不存在两个/,可以用 SName+'/' 替换 SName.
select SUBSTRING(sname,1,PATINDEX('%/%',sname))+
SUBSTRING(SUBSTRING(sname,LEN(SUBSTRING(sname,1,PATINDEX('%/%',sname)))+1,LEN(sname)),1,PATINDEX('%/%',SUBSTRING(sname,LEN(SUBSTRING(sname,1,PATINDEX('%/%',sname)))+1,LEN(sname)))-1)
from [huang]
select substring(
[SName],
1,
(
charindex
(
'\',
[SName],
(charindex('\',[SName])+1)
)-1
)
)
from [huang]
--------------------------------------
123\1xx
123\2xx
123\3xx
123\4xx
123\5xx
123\1xx
123\2xx
123\3xx
(8 行受影响)
用老黄的测试数据试了一下,没有你说的错误啊....----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-04 15:20:54
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([SName] nvarchar(38),[ID] int)
insert [huang]
select '123\1xx\2aa\4\',101 union all
select '123\2xx\2a1\304\',102 union all
select '123\3xx\2a3\355\',103 union all
select '123\4xx\2a55\320\',104 union all
select '123\5xx\2a2\302\',105 union all
select '123\1xx\2aa\4\899\3',101 union all
select '123\2xx\2a1\304\',102 union all
select '123\3xx\2a3\355\',103
--------------生成数据--------------------------
select SUBSTRING(sname,1,PATINDEX('%\%',sname))+
SUBSTRING(SUBSTRING(sname,LEN(SUBSTRING(sname,1,PATINDEX('%\%',sname)))+1,LEN(sname)),1,PATINDEX('%\%',SUBSTRING(sname,LEN(SUBSTRING(sname,1,PATINDEX('%\%',sname)))+1,LEN(sname)))-1),
ID
from [huang]
----------------结果----------------------------
/*
---------------------------------------------------------------------------- -----------
123\1xx 101
123\2xx 102
123\3xx 103
123\4xx 104
123\5xx 105
123\1xx 101
123\2xx 102
123\3xx 103
*/