mysql日期格式与字符串格式的转化
2012-06-11 14:12
225 查看
直接贴代码吧,例子在代码里,本例经过测试。
总结:
字符串转日期:
str_to_date(concat(substr(ctiWorkEventTbl.hourValue, 1, 4), '-',substr(ctiWorkEventTbl.hourValue, 5, 2),'-',substr(ctiWorkEventTbl.hourValue, 7, 2)), '%Y-%m-%d') strdate
简化就是 str_to_date(‘2012-03-06’,'%Y-%m-%d') strdate
日期格式的格式化:
date_format('2012-05-03','%y-%m-%d')
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
select * from ( select str_to_date(concat(substr(ctiWorkEventTbl.hourValue, 1, 4), '-',substr(ctiWorkEventTbl.hourValue, 5, 2),'-',substr(ctiWorkEventTbl.hourValue, 7, 2)), '%Y-%m-%d') strdate , concat(substr(ctiWorkEventTbl.hourValue, 1, 4), '-',substr(ctiWorkEventTbl.hourValue, 5, 2),'-',substr(ctiWorkEventTbl.hourValue, 7, 2)) ,ctiWorkEventTbl.hourValue ,ctiWorkEventTbl.agentCnt ,ctiCallstaTbl.totalCallCnt/ctiWorkEventTbl.agentCnt as perAgtCallCnt /*坐席平均呼叫次数*/ ,ctiCallstaTbl.totalCallCnt ,ctiCallstaTbl.totalOkCallCnt ,ctiCallstaTbl.totalOkCallCnt/ctiWorkEventTbl.agentCnt as perAgtOkCallCnt /*平均通话次数*/ ,ctiCallstaTbl.totalBadCallCnt ,ctiCallstaTbl.badToAgtCnt ,ctiCallstaTbl.userAbandonCnt ,ctiCallstaTbl.callDuration ,ctiCallstaTbl.perCallDuration ,ctiCallstaTbl.ringCnt ,ctiCallstaTbl.ringDuration ,ctiCallstaTbl.perRingDuration ,ctiWorkEventTbl.refer2OtherCnt ,ctiWorkEventTbl.transfer2OtherCnt ,ctiCallstaTbl.agentDisconnectCnt ,ctiCallstaTbl.userDisconnectCnt ,ctiCallstaTbl.otherGrpCallInCnt ,ctiCallstaTbl.perOtherGrpCallInCnt ,ctiCallstaTbl.otherGrpCallInDuration ,ctiCallstaTbl.maxCallDuration from ( select substr(starttime, 1, 8) as hourValue , concat(substr(starttime, 1, 4), '-',substr(starttime, 5, 2),'-',substr(starttime, 7, 2)) starttimestr ,sum(if(calltype = 3 && direction = 1, 1, 0)) as totalCallCnt /*总呼叫次数*/ ,sum(if(calltype = 3 && direction = 1 && status = 1, 1, 0)) as totalOkCallCnt /*通话次数*/ ,sum(if(calltype = 3 && direction = 1 , 1, 0)) as totalCallInCnt /*呼入次数*/ ,sum(if(calltype = 3 && direction = 1 && status = 2 , 1, 0)) as totalBadCallCnt /*总失败次数*/ ,sum(if(calltype = 3 && detail = 0007 , 1, 0)) as badToAgtCnt /*坐席未接起*/ ,sum(if(calltype = 3 && (detail = 0008 || detail = 0106 || detail = 0107), 1, 0)) as userAbandonCnt /*用户放弃*/ ,sum(if(calltype = 3 && detail = 0009 , 1, 0)) as agentNoAnswerCnt /*坐席无应答*/ ,sum(if(calltype = 3 && direction = 1 && status = 1, duration, 0)) as callDuration /*通话时长*/ ,sum(if(calltype = 3 && direction = 1 && status = 1, duration, 0))/sum(if(calltype = 3 && direction = 1 && status = 1, 1, 0)) as perCallDuration /*平均通话时长*/ ,sum(if(calltype = 3 && (direction = 1 || direction = 2 ) && ringseconds > 0, 1, 0)) as ringCnt /*振铃次数*/ ,sum(if(calltype = 3 && (direction = 1 || direction = 2 ) && ringseconds > 0, ringseconds, 0)) as ringDuration /*振铃时长*/ ,sum(if(calltype = 3 && (direction = 1 || direction = 2 ) && ringseconds > 0, ringseconds, 0))/sum(if(calltype = 3 && (direction = 1 || direction = 2 ) && ringseconds > 0, 1, 0)) as perRingDuration /*平均振铃时长*/ ,sum(if((calltype = 3 && direction = 1 && disconnection = 2) || (calltype = 3 && direction = 2 && disconnection = 1) , 1, 0)) as agentDisconnectCnt /*坐席挂机*/ ,sum(if((calltype = 3 && direction = 1 && disconnection = 1) || (calltype = 3 && direction = 2 && disconnection = 2) , 1, 0)) as userDisconnectCnt /*用户挂机*/ ,sum(if(calltype = 3 && direction = 1 && substr(taskid, 9, 6) != substr(department, 15, 6), 1, 0)) as otherGrpCallInCnt /*非本组代答*/ ,sum(if(calltype = 3 && direction = 1 && substr(taskid, 9, 6) != substr(department, 15, 6), 1, 0)) / sum(if(calltype = 3 && direction = 1, 1, 0)) as perOtherGrpCallInCnt /*非本组代答率*/ ,sum(if(calltype = 3 && direction = 1 && substr(taskid, 9, 6) != substr(department, 15, 6), duration, 0)) as otherGrpCallInDuration /*非本组代答时长*/ ,max(if(calltype = 3 && direction = 1 && status = 1, duration, 0)) as maxCallDuration /*最长通话时长*/ from cti_callstat group by substr(starttime, 1, 8) ) ctiCallstaTbl left join ( select count(distinct(agentid))as agentCnt ,substr(etime, 1, 8) as hourValue ,sum(if(event = 5, 1, 0)) as refer2OtherCnt /*咨询次数*/ ,sum(if(event = 6 || event = 7 , 1, 0)) as transfer2OtherCnt /*转移次数*/ from cti_workevent group by substr(etime, 1, 8) ) ctiWorkEventTbl on ctiCallstaTbl.hourValue = ctiWorkEventTbl.hourValue ) d where d.strdate > date_format('2012-05-03','%y-%m-%d')
总结:
字符串转日期:
str_to_date(concat(substr(ctiWorkEventTbl.hourValue, 1, 4), '-',substr(ctiWorkEventTbl.hourValue, 5, 2),'-',substr(ctiWorkEventTbl.hourValue, 7, 2)), '%Y-%m-%d') strdate
简化就是 str_to_date(‘2012-03-06’,'%Y-%m-%d') strdate
日期格式的格式化:
date_format('2012-05-03','%y-%m-%d')
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
相关文章推荐
- 字符串日期转化为java日期格式
- mysql日期和字符串相互转化
- 将SPSS中字符串格式的数据转化成日期格式
- 日期格式与字符串的相互转化
- mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期格式
- 将日期格式转化成字符串
- jsp sql server 2000的时间日期格式与字符串相转化
- 对输入的字符串类型的日期进行一定格式的日期转化
- Java如何将指定字符串转化为指定日期格式
- 在mysql里面利用str_to_date()把字符串转换为日期格式
- Mysql中如何将时间戳转化为常用日期格式
- MySQL-日期字符串(****年**月**日 格式)转Date类型
- Java将字符串转化为时间格式,与实现日期的计算
- java-将字符串和毫秒值转化为日期格式的几种方法
- 利用springMVC将字符串转化为日期格式
- 将某个日期以固定格式转化成字符串
- Mysql中如何将时间戳转化为常用日期格式
- 将未指定格式的日期字符串转化成java.util.Date类型日期对象
- MySQL 日期格式的转化
- 按照要求的输入输出格式进行日期字符串的转化