1、oracle时间类型的使用
--时间类型的使用
select * from bureau_info where bur_end_date > to_date('2011-09-30','yyyy-mm-dd');
--获取当前时间
select to_char(sysdate,'yyyy') from dual;
select sysdate from files; --files是用户创建的任意一张表=,表中不包含sysdate字段
--计算两时间差精确到天
select to_date(bur_end_date)-to_date(bur_begin_date) from bureau_info where rownum = 1;
--得到当前时间是当前年的那一个星期
select to_char(sysdate,'WW') from dual
--取本周第一天喝最后一天的两种写法
select count(*) from bureau_info where to_char(update_date,'yyyy') = to_char(sysdate,'yyyy') and
to_char(update_date,'ww') = to_char(sysdate+1,'WW')
select count(*) from bureau_info where update_date between trunc(sysdate,'day')+1 and trunc(sysdate,'day') + 8;
--得到当前月份的第一天和最后一天
select to_char(sysdate,'yyyy/mm')||'/01' firstday, to_char(last_day(sysdate),'yyyy/mm/dd') lastday from dual
--时间约束
create table c (
sss date,
constraint ck_sss check(sss >to_date('2011-09-30','yyyy-mm-dd') and sss < to_date('2012-09-30','yyyy-mm-dd'))
)
--oracle常用数据类型
常用的数据库字段类型如下:
字段类型 中文说明 限制条件 其它说明
CHAR 固定长度字符串 最大长度2000 bytes
VARCHAR2 可变长度的字符串 最大长度4000 bytes 可做索引的最大长度749
NCHAR 根据字符集而定的固定长度字符串 最大长度2000 bytes
NVARCHAR2 根据字符集而定的可变长度字符串 最大长度4000 bytes
DATE 日期(日-月-年) DD-MM-YY(HH-MI-SS) 经过严格测试,无千虫问题
LONG 超长字符串 最大长度2G(231-1) 足够存储大部头著作
RAW 固定长度的二进制数据 最大长度2000 bytes 可存放多媒体图象声音等
LONG RAW 可变长度的二进制数据 最大长度2G 同上
BLOB 二进制数据 最大长度4G
CLOB 字符数据 最大长度4G
NCLOB 根据字符集而定的字符数据 最大长度4G
BFILE 存放在数据库外的二进制数据 最大长度4G
ROWID 数据表中记录的唯一行号 10 bytes ********.****.****格式,*为0或1
NROWID 二进制数据表中记录的唯一行号 最大长度4000 bytes
NUMBER(P,S) 数字类型 P为整数位,S为小数位
DECIMAL(P,S) 数字类型 P为整数位,S为小数位
INTEGER 整数类型 小的整数
FLOAT 浮点数类型 NUMBER(38),双精度
REAL 实数类型 NUMBER(63),精度更高
2、自定义排序,bur_state为列名;启动'。。。。为列的值
order by DECODE(bur_state, '启动',1,'结束',2,'中止',3,'跟踪',4,'准备',5)
3、sql语句中时间字段查询,在字段上不用函数,而是在字符串上用函数
oracle,时间类型不用函数的比较:
alarmtime between to_date(dqksTime, 'YYYYMMDDHH24mi') and to_date(dqjsTime, 'YYYYMMDDHH24mi')
oracle,时间比较中时间的计算:
alarmtime between to_date(dqksTime, 'YYYYMMDDHH24mi') - 2 and to_date(dqksTime, 'YYYYMMDDHH24mi')
说明:alarmtime 表字段中时间类型的字段 |