`
lushuaiyin
  • 浏览: 674977 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mysql日期格式与字符串格式的转化

 
阅读更多

直接贴代码吧,例子在代码里,本例经过测试。

 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), 这里星期一是星期的第一天
%% 一个文字“%”。

分享到:
评论

相关推荐

    mysql字符串转换日期.sql

    mysql字符串转换日期

    mysql 日期与时间的转换

    里面详细介绍了mysql中日期与时间戳的转换

    详解MySQL日期 字符串 时间戳互转

    date_format(date, format) 函数,MySQL日期格式化函数date_format() unix_timestamp() 函数 str_to_date(str, format) 函数 from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函

    MySQL replace函数替换字符串语句的用法

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望... REPLACE(str,from_str,to_str) 在字符串 str 中所有出现的字符串 from_str 均被 to_str替换,然后返回这个字符串: mysql> SELECT

    mysql的日期和时间函数

    注意,如果依着包含一个时间部分的间隔增加或减少一个日期值,该日期值将被自动地转换到一个日期时间值: mysql> SELECT DATE_ADD("1999-01-01", INTERVAL 1 DAY); -> 1999-01-02mysql> SELECT DATE_ADD("1999-01-...

    mysql资料_导入导出_备份还原_编码_字段类型长度_字符串_长度-定位-截取-拼接_日期_求差-加减-UTC-时区转换_查看表空间大小

    资源包含文件: 1、MySQL命令行导出数据库.txt 2、MySQL字段数据类型和长度.sql 3、usefullSQL.sql ...字符串:求长度,定位,截取,拼接 日期操作:求差,加减,UTC时间,时区(timezone)转换等 查看表空间大小

    公共方法类库dll,转换文件格式,处理字符串,处理图片,处理文件等

    13.StringDeal.cs 专门用于对于字符串的处理如截取、过滤、数据类型转换等操作 14.SystemConfigInfo.cs 专门用于得到操作系统的相应数据如CUP使用率、硬盘使用情况等 15.ValidatePost.cs 专门用于数据的验证如邮箱...

    mysql常用函数实例总结【聚集函数、字符串、数值、时间日期处理等】

    合并字符串函数:concat(str1,str2,str3…) 比较字符串大小函数:strcmp(str1,str2) 获取字符串字节数函数:length(str) 获取字符串字符数函数:char_length(str) 字母大小写转换函数:大写:upper(x),ucase(x)...

    详解MySQL中concat函数的用法(连接字符串)

    一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col) MySQL的concat函数可以连接一个或者多个字符串,如 mysql> ...

    FROM_UNIXTIME 格式化MYSQL时间戳函数

    返回表示 Unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。 根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字...

    Mysql 数字类型转换函数

    1、将Int 转为varchar经常用 concat函数,比如concat(8,’0′) 得到字符串 ’80’ 2、将varchar 转为Int 用 cast(a as signed) a为varchar类型的字符串 总结:类型转换和SQL Server一样,就是类型参数有点点不同 : ...

    MySQL常用类型转换函数总结(推荐)

    连接字符串常用:concat函数。如sql查询条件的like查询, AND c.name like concat(#{param.name},’%’) 将Int 转为varchar经常用 concat函数,比如concat(8,’0′) 得到字符串 ’80’  2、Cast函数;CONVERT函数...

    MYSQL

    7.4.7 类型转换运算符 7.4.8 控制流函数 7.4.9 数学函数 7.4.10 字符串函数 7.4.11 日期和时间函数 7.4.12 其他函数 7.4.13 与GROUP BY子句一起使用的函数 7.5 CREATE DATABASE ...

    深入MYSQL字符数字转换的详解

    在进行ifnull处理时,比如 ifnull(a/b,’0′) 这样就会导致 a/b成了字符串,因此需要把’0’改成0,即可解决此困扰3.比较数字和varchar时,比如a=11,b=”11ddddd”;则 select 11=”11ddddd”相等若绝对比较可以这样...

    MYSQL,SQLSERVER,ORACLE常用的函数

    返回一个与给定的字符串读音相同的字符串 SQL> create table table1(xm varchar(8)); SQL> insert into table1 values('weather'); SQL> insert into table1 values('wether'); SQL> insert into table1 values...

    MySQL 5.1中文手冊

    8.14. replace:字符串替换实用工具 8.15. mysql_zap:杀死符合某一模式的进程 9. 语言结构 9.1. 文字值 9.1.1. 字符串 9.1.2. 数值 9.1.3. 十六进制值 9.1.4. 布尔值 9.1.5. 位字段值 9.1.6. NULL值 9.2. 数据库、...

    PHP和MySQL Web开发第4版pdf以及源码

    4.2.3 格式化字符串以便存储:addslashes()和stripslashes() 4.3 用字符串函数连接和分割字符串 4.3.1 使用函数explode()、implode()和join() 4.3.2 使用strtok()函数 4.3.3 使用substr()函数 4.4 字符串的比较...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    varchar2 1~4000字节 可变长度字符串,与CHAR类型相比,使用VARCHAR2可以节省磁盘空间,但查询效率没有char类型高 数值类型 Number(m,n) m(1~38) n(-84~127) 可以存储正数、负数、零、定点数和精度为38位的浮点数...

    MySQL中文参考手册.chm

    7.4.4 逻辑运算 7.4.5 比较运算符 7.4.6 字符串比较函数 7.4.7 类型转换运算符 7.4.8 控制流函数 7.4.9 数学函数 7.4.10 字符串函数 7.4.11 日期和时间函数 7.4.12 ...

Global site tag (gtag.js) - Google Analytics