CHARINDEX 详解及应用

CHARINDEX
返回字符串中指定表达式的起始位置。
语法
CHARINDEX ( e瓁pression1 , e瓁pression2 [ , start_location ] )
参数
e瓁pression1
一个表达式,其中包含要寻找的字符的次序。e瓁pression1 是一个短字符数据类型分类的表达式。
e瓁pression2
一个表达式,通常是一个用于搜索指定序列的列。e瓁pression2 属于字符串数据类型分类。
start_location
在 e瓁pression2 中搜索 e瓁pression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 e瓁pression2 的起始位置开始搜索。
返回类型
int
注释
如果 e瓁pression1 或 e瓁pression2 之一属于 Unicode 数据类型(nvarchar 或 nchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。
如果 e瓁pression1 或 e瓁pression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 e瓁pression1 和 e瓁pression2 都为 NULL 时返回 NULL 值。
如果在 e瓁pression2 内没有找到 e瓁pression1,则 CHARINDEX 返回 0。

SELECT c_cpc--,SUBSTRING(SUBSTRING(c_cpc,CHARINDEX('|',c_cpc)+1,LEN(c_cpc)),CHARINDEX('|',c_cpc),CHARINDEX('|',c_cpc)-1)
FROM b_company where len(c_cpc)>1

select charindex('|',c_cpc),c_cpc,
substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),
substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))) as subend
--substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1)))
from b_company where len(c_cpc)>1 and charindex('|',subend)=0


SELECT c_cpc,REPLACE(SUBSTRING(SUBSTRING(c_cpc,CHARINDEX('|',c_cpc),LEN(c_cpc)),CHARINDEX('|',c_cpc)+1
,CHARINDEX('|',c_cpc)-1),'|','')
FROM b_company


create view oldview
as
select c_id,charindex('|',c_cpc) indexmark,c_cpc,
substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1) indexone,
substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))) as subend
--substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1)))
from b_company where len(c_cpc)>1

----valiage
create view oldarea
as
select c_id,charindex('|',c_cpc) indexmark,c_cpc,
substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1) indexone,
substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))) as subend,
substring(substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))),charindex('|',substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))))+1,len(substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))))-charindex('|',substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))+1,len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1))))+1) as area
--substring(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),charindex('|',substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1),len(substring(c_cpc,charindex('|',c_cpc)+1,len(c_cpc)-charindex('|',c_cpc)+1)))
from b_company where len(c_cpc)>1

select * from oldview where charindex('|',subend)>0

select * from oldview where

select * from province
select * from city


-----///1sql只有省市---正确
select province+'-'+city area,c_id from oldview a,city b,province c where b.father=c.id and a.subend=cast(b.id as varchar(50)) and charindex('|',subend)<=0

-----2sql有区的----正确
select province+'-'+city+'-'+villagename area,c_id from oldarea a,city b,province c,village d where d.id=b.id and b.father=c.id and a.area=cast(d.villageid as varchar(50)) and charindex('|',subend)>0 and area like '[0-9]%'
-----3sql关联不上区的--正确
select '浙江省-杭州',c_id from oldarea where charindex('|',subend)>0 and area like '[0-9]%' and c_id not in(select c_id from oldarea a,city b,province c,village d where d.id=b.id and b.father=c.id and a.area=cast(d.villageid as varchar(50)) and charindex('|',subend)>0 and area like '[0-9]%')


---4sql区名不是以数字打头得----5709条
select * from oldarea where charindex('|',subend)>0 and area like '[^0-9]%'
---1.323条
--select * from oldarea where charindex('|',subend)>0 and area like '[^0-9]%'
select province+'--'+city as area,c_id from oldarea a,city b,province c where b.father=c.id and substring(subend,0,charindex('|',subend))=cast(b.id as varchar(50)) and charindex('|',subend)>0 and area like '[^0-9]%' and subend like '[0-9]%'
---2.5386
select province+'-'+rtrim(replace(subend,'|','-')) as area,c_id from oldarea a,province b where substring(indexone,0,charindex('|',indexone))=b.id and charindex('|',subend)>0 and area like '[^0-9]%'and subend like '[^0-9]%'

---5sql有区位置但是没有地区编号及地区编号为空得---正确
select * from oldarea where charindex('|',subend)>0 and len(area)=0 ----三种1.除去|关联市2.只有省1.汉字2.数字共计256条
---1.除去|关联市 213条
select province+'-'+city area,c_id from oldarea a,city b,province c where b.father=c.id and ltrim(rtrim(replace(a.subend,'|','')))=cast(b.id as varchar(50)) and charindex('|',subend)>0 and len(area)=0
---2.1.汉字 23条
select substring(replace(replace(c_cpc,'|',''),'中国',''),0,len(replace(c_cpc,'|','-'))-1) area,c_id from oldarea where charindex('|',subend)>0 and len(area)=0 and indexone like '[^0-9]%'
---3.1 数字的
select province as area,c_id from oldarea a,province b where replace(a.indexone,'||','')=b.id and charindex('|',subend)>0 and len(area)=0 and charindex('||',indexone)>0 and indexone like '[0-9]%'

--6sql 有区位置但是没有地区编号及地区编号为空得 -----正确
select substring(replace(c_cpc,'|','-'),0,len(replace(c_cpc,'|','-'))) as area,c_id from oldarea where charindex('|',subend)>0 and c_id not in(select c_id from oldarea where charindex('|',subend)>0 and len(area)=0 union select c_id from oldarea where charindex('|',subend)>0 and area like '[^0-9]%' union select c_id from oldarea where charindex('|',subend)>0 and area like '[0-9]%')

-----大结局
create view allplace
as
select province+'-'+city area,c_id from oldview a,city b,province c where b.father=c.id and a.subend=cast(b.id as varchar(50)) and charindex('|',subend)<=0
union
select province+'-'+city+'-'+villagename area,c_id from oldarea a,city b,province c,village d where d.id=b.id and b.father=c.id and a.area=cast(d.villageid as varchar(50)) and charindex('|',subend)>0 and area like '[0-9]%'
union
select '浙江省-杭州' area,c_id from oldarea where charindex('|',subend)>0 and area like '[0-9]%' and c_id not in(select c_id from oldarea a,city b,province c,village d where d.id=b.id and b.father=c.id and a.area=cast(d.villageid as varchar(50)) and charindex('|',subend)>0 and area like '[0-9]%')
union
select province+'--'+city as area,c_id from oldarea a,city b,province c where b.father=c.id and substring(subend,0,charindex('|',subend))=cast(b.id as varchar(50)) and charindex('|',subend)>0 and area like '[^0-9]%' and subend like '[0-9]%'
union
select province+'-'+rtrim(replace(subend,'|','-')) as area,c_id from oldarea a,province b where substring(indexone,0,charindex('|',indexone))=b.id and charindex('|',subend)>0 and area like '[^0-9]%'and subend like '[^0-9]%'
union
select province+'-'+city area,c_id from oldarea a,city b,province c where b.father=c.id and ltrim(rtrim(replace(a.subend,'|','')))=cast(b.id as varchar(50)) and charindex('|',subend)>0 and len(area)=0
union
select substring(replace(replace(c_cpc,'|',''),'中国',''),0,len(replace(c_cpc,'|','-'))-1) area,c_id from oldarea where charindex('|',subend)>0 and len(area)=0 and indexone like '[^0-9]%'
union
select province as area,c_id from oldarea a,province b where replace(a.indexone,'||','')=b.id and charindex('|',subend)>0 and len(area)=0 and charindex('||',indexone)>0 and indexone like '[0-9]%'
union
select substring(replace(c_cpc,'|','-'),0,len(replace(c_cpc,'|','-'))) as area,c_id from oldarea where charindex('|',subend)>0 and c_id not in(select c_id from oldarea where charindex('|',subend)>0 and len(area)=0 union select c_id from oldarea where charindex('|',subend)>0 and area like '[^0-9]%' union select c_id from oldarea where charindex('|',subend)>0 and area like '[0-9]%')

select * from allplace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值