数据库技术,mysql,oracle
oracle:各种不同日期时间字段格式的比较和处理
日期时间字段格式通常有:varchar字符串,date日期,timestamp(number)时间戳
to_date把字符串转换为日期,两个参数的格式要完全一样;
to_char把日期格式转为字符串,第一个参数必须是date格式的,若是varchar,需要先用to_date转换为date格式;
to_date是类型转换,不是格式转换,字符串和后面的格式要匹配;to_char才是转成对应格式;
时间日期字段名:logtime,
需求A:获取2017-05-02至2017-05-15的数据;
需求B:获取9:00至12:00的数据;
一、字符串格式日期logtime
A: select * from tb where logtime>='2017-05-02' and logtime<='2017-05-15'; B: select * from tb where to_char(to_date(logtime,'yyyy-mm-dd hh24:mi:ss'),'hh24:mi:ss')>='9:00:00' and to_char(to_date(logtime,'yyyy-mm-dd hh24:mi:ss'),'hh24:mi:ss')<='12:59:59';
二、date格式日期logtime
A: select * from tb where to_char(logtime,'yyyy-mm-dd')>='2017-05-02' and to_char(logtime,'yyyy-mm-dd')<='2017-05-15'; B: select * from tb where to_char(logtime,'hh24:mi:ss')>='9:00:00' and to_char(logtime,'hh24:mi:ss')<='12:59:59';