您的位置:首页 > 数据库 > Oracle

oracle中字符串与时间的转换

2015-04-15 14:15 363 查看
Oracle的Replace函数与translate函数详解与比较:

Replace:字符串级别的代替

SELECT REPLACE('2016年01月01日','年','-') from dual --> 2016-01月01日

translate:字符级别的代替

SELECT translate('2016年01月01日','年月日','--') from dual --> 2016-01-01

Oracle SQL中实现indexOf和lastIndexOf功能:

这里是substr和instr的综合使用。

substr(stirng1,strat,length);截取功能;

解析一下:string1是你要截取的字符串,strat是要开始截取的位置,length是你要截取多少个字符串。

instr(string1,string2,end);是检索功能,

解析一下:string1是你要检索的字符串,string2是你在string1中要查找的字符串,end的意思1代表从字符串的开始检索,-1代表中字符串的末尾开始检索。

例如:现在想要把字符串‘'2016年01月01日 星期一'’,转化为‘2016-01-01’ 类型的,你就可以如下写:

SELECT translate(substr('2016年01月01日 星期一',0,instr('2016年01月01日 星期一',' ',1) ) ,'年月日','--') from dual --> 2016-01-01

SELECT to_date( translate(substr('2016年01月01日 星期一',0,instr('2016年01月01日 星期一',' ',1) ) ,'年月日','--'),'yyyy-mm-dd hh24:mi:ss') from dual --> 2016/1/1(时间类型)

//我的示例代码:

select t.*,d.* from CRM_ttt t inner join CRM_ddd d

on t.companyid = d.companyid

WHERE to_date(to_char(SYSDATE+to_number(startdatas),'yyyy-mm-dd'),'yyyy-mm-dd')>=to_date(translate(substr(startdate,0,instr(startdate,' ',1)),'年月日','--'),'yyyy-mm-dd')

AND to_date(to_char(SYSDATE ,'yyyy-mm-dd'),'yyyy-mm-dd')<=to_date(translate(substr(startdate,0,instr(startdate,' ',1)),'年月日','--'),'yyyy-mm-dd')

AND d.remindtype = '循环提醒'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: