今天接到一个需求,统计一下邮箱系统一至九月分的用户开销户记录,开户记录很容易就查到了,但由于用户销户后数据库表中找不到对应的用户信息,查了一系列的表,发现SBMailSubUserLog其中一列中包含有用户的部分信息,如果想要使用的话必须截取,然后我们步入正题,截取数据串。
原始的字符串‘邮箱成功开通
Passwd=Nm,12345,Mailsize=30,Mailname=gtxx_lijie,Displayname=李洁,saa
现在我们把MailnameDisplayname提取出来,以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(subdescpatindex('%,%', 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