SQL字符串截取
原创
©著作权归作者所有:来自51CTO博客作者zhiliaozhixin的原创作品,如需转载,请与作者联系,否则将追究法律责任
今天接到一个需求,统计一下邮箱系统一至九月分的用户开销户记录,开户记录很容易就查到了,但由于用户销户后数据库表中找不到对应的用户信息,查了一系列的表,发现SBMailSubUserLog其中一列中包含有用户的部分信息,如果想要使用的话必须截取,然后我们步入正题,截取数据串。
原始的字符串‘邮箱成功开通
Passwd=Nm,12345,Mailsize=30,Mailname=gtxx_lijie,Displayname=李洁,saa’
现在我们把Mailname和Displayname提取出来,以Displayname为例,大体思路是这样:把‘Displayname=’和前面的数据截取掉,留下后面的数据,
SQL:
Select SUBSTRING(subdesc,CHARINDEX('DISPLAYNAME',subdesc)+12,LEN(subdesc)-CHARINDEX('DISPLAYNAME',subdesc)) from SBMailSubUserLog where subtype like '%员工业务开通%' and mobile in (select distinct mobile from SBMailSubUserLog where subtype like '%员工业务关闭%' and changedate>='2009-01-01 00:32:24.327')
显示出来的就是这样’ 李洁,saa’现在再整理‘,’号之后的数据select left(subdesc,patindex('%,%', subdesc)) from SBMailSubUserLog,然后把它们整合起来,
Select left(SUBSTRING(subdesc,CHARINDEX('DISPLAYNAME',subdesc)+12,LEN(subdesc)-CHARINDEX('DISPLAYNAME',subdesc)),patindex('%,%',SUBSTRING(subdesc,CHARINDEX('DISPLAYNAME',subdesc)+12,LEN(subdesc)-CHARINDEX('DISPLAYNAME',subdesc))))
from SBMailSubUserLog
where subtype like '%员工业务开通%' and
mobile in
(select distinct mobile from SBMailSubUserLog where subtype like '%员工业务关闭%' and changedate>='2009-01-01 00:32:24.327')。
完整的提取数据的SQL:
select distinct a.mobile,b.displayname,b.mailname,
datepart(year,a.changedate)*10000+datepart(month,a.changedate)*100+datepart(day,a.changedate) as changedate2,
a.source,a.subtype,a.subdesc
from SBMailSubUserLog a join
(select mobile,
left(SUBSTRING(subdesc,CHARINDEX('DISPLAYNAME',subdesc)+12,LEN(subdesc)-CHARINDEX('DISPLAYNAME',subdesc)),patindex
('%,%',SUBSTRING(subdesc,CHARINDEX('DISPLAYNAME',subdesc)+12,LEN(subdesc)-CHARINDEX('DISPLAYNAME',subdesc)))) as displayname,
left(SUBSTRING(subdesc,CHARINDEX('mailname',subdesc)+12,LEN(subdesc)-CHARINDEX('mailname',subdesc)),patindex
('%,%',SUBSTRING(subdesc,CHARINDEX('mailname',subdesc)+12,LEN(subdesc)-CHARINDEX('mailname',subdesc)))) as mailname,
source,subtype,datepart(year,changedate)*10000+datepart(month,changedate)*100+datepart(day,changedate) as changedate1
from SBMailSubUserLog
where subtype like '%员工业务开通%' and
mobile in
(select distinct mobile from SBMailSubUserLog where subtype like '%员工业务关闭%' and changedate>='2009-01-01 00:32:24.327')
) b on a.mobile=b.mobile
where a.subtype like '%员工业务关闭%'
--and a.changedate>='2009-01-01 00:32:24.327'
and datepart(year,a.changedate)*100+datepart(month,a.changedate)='200904'
order by changedate2
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Oracle sql语句练习
先创建下面三个表: &nb
oracle 数据库 休闲 sql语句 练习题 -
oracle sql语句大全
ORACLE支持五种类型的完整性约束NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况
sql语句大全 操作符 oracle 数据 -
Oracle 导入SQL语句
第三步:图示
数据库 SQL 命令窗口