数据库时间

2017-09-18 23:08:12 jazdbmin1639整理 数据库时间 数据库时间

数据库时间和系统时间

Q1: sql数据库中的时间类型

日期时间类型:time:

格式:hh:mm:ss[.nnnnnnn]

范围:00:00:00.0000000 到23:59:59.9999999

精确度:100 纳秒

存储大小(以字节为单位):3-5

用户定义的秒的小数精度:有

时区偏移量:无

date:

格式:YYYY-MM-DD

范围:0001-01-01 到 9999-12-31

精确度:1天

存储大小(以字节为单位):3

用户定义的秒的小数精度:无

时区偏移量:无

smalldatetime:

格式:YYYY-MM-DD hh:mm:ss

范围:1900-01-01 到 2079-06-06

精确度:1分钟

存储大小(以字节为单位):4

用户定义的秒的小数精度:无

时区偏移量:无

datetime:

格式:YYYY-MM-DD hh:mm:ss[.nnn]

范围:1753-01-01 到 9999-12-31

精确度:0.00333秒

存储大小(以字节为单位):8

用户定义的秒的小数精度:无

时区偏移量:无

datetime2:

格式:YYYY-MM-DD hh:mm:ss[.nnnnnnn]

范围:0001-01-01 00:00:00.0000000 到9999-12-31 23:59:59.9999999

精确度:100钠秒

存储大小(以字节为单位):6 到 8

用户定义的秒的小数精度:有

时区偏移量:无

datetimeoffset:

格式:YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm

范围:0001-01-01 00:00:00.0000000 到9999-12-31 23:59:59.9999999(以UTC 时间表示)

精确度:100钠秒

存储大小(以字节为单位):8 到10

用户定义的秒的小数精度:有

时区偏移量:有

Q2: 如何插入时间日期型数据在数据库里?

以目前三大主流数据库oracle,mysql,sqlserver分别说明。

如果是oracle,需要用to_date函数将字符型数据转换,插入到数据库的日期型数据中。

如:

createtabletest
(cdatedate);
insertintotestvalues(to_date('2015-08-13','yyyy-mm-dd'));
commit;

如果是mysql或者sqlserver,可直接以字符形式插入,数据库会自动将字符转成日期。

createtabletest
(cdatedatetime);
insertintotestvalues('2015-08-13');

需要注意:oracle中插入后需要commit(提交),否则在关闭当前会话后,插入是不成功的。

Q3: 数据库里的时间格式

方法一:通过函数to_char实现时间格式的转换
SQL> select sysdate from dual;
SYSDATE
------------
17-JUN-15
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YYYY-MM-DDHH24:MI:SS'
--------------------------------------
2015-06-17 14:08:21
方法二:更改会话参数
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
方法二:更改参数文件
可以在 init.ora 中加上一行
nls_date_format='yyyy-mm-dd hh24:mi:ss'
或者
alter system set nls_date_format='yyyy-mm-dd hh24:mi:ss’ scope=spfile;

Q4: oracle数据库时间日期查询

TO_DATE格式(以时间:2007-11-0213:45:25为例)
Year:
yy two digits 两位年显示值:07
yyy three digits 三位年显示值:007
yyyy four digits 四位年显示值:2007
Month:
mmnumber两位月显示值:11
monabbreviated 字符集表示显示值:11月,若是英文版,显示nov
month spelled out 字符集表示显示值:11月,若是英文版,显示november
Day:
ddnumber当月第几天显示值:02
dddnumber当年第几天显示值:02
dyabbreviated 当周第几天简写显示值:星期五,若是英文版,显示fri
dayspelled out当周第几天全写显示值:星期五,若是英文版,显示friday
ddspth spelled out, ordinal twelfth
Hour:
hhtwo digits 12小时进制显示值:01
hh24 two digits 24小时进制显示值:13
Minute:
mitwo digits 60进制显示值:45
Second:
sstwo digits 60进制显示值:25
其它
Qdigit季度显示值:4
WWdigit当年第几周显示值:44
Wdigit当月第几周显示值:1
24小时格式下时间范围为: 0:00:00 - 23:59:59....
12小时格式下时间范围为: 1:00:00 - 12:59:59 ....
1. 日期和字符转换函数用法(to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;//日期转化为字符串
select to_char(sysdate,'yyyy') as nowYearfrom dual;//获取时间的年
select to_char(sysdate,'mm')as nowMonth from dual;//获取时间的月
select to_char(sysdate,'dd')as nowDayfrom dual;//获取时间的日
select to_char(sysdate,'hh24') as nowHourfrom dual;//获取时间的时
select to_char(sysdate,'mi')as nowMinute from dual;//获取时间的分
select to_char(sysdate,'ss')as nowSecond from dual;//获取时间的秒
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')from dual//
2.
select to_char( to_date(222,'J'),'Jsp') from dual
显示Two Hundred Twenty-Two
3.求某天是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
星期一
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
monday
设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 时间为null的用法
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6.月份差
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。
所以,当时间需要精确的时候,觉得to_char还是必要的
7. 日期格式冲突问题
输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中写
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,
可查看
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
8.
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
查找2002-02-28至2002-02-01间除星期一和七的天数
在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒).
9. 查找月份
select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
10. Next_day的用法
Next_day(date, day)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第一条记录的TIME 与最后一行是一样的
可以建立一个函数来处理这个问题
create or replace function sys_date return date is
begin
return sysdate;
end;
select to_char(sys_date,'hh:mi:ss') from all_objects;
12.获得小时数
extract()找出日期或间隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL> select sysdate ,to_char(sysdate,'hh') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
SQL> select sysdate ,to_char(sysdate,'hh24') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
13.年月日的处理
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from (
select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp
)
)
14.处理月份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16.找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
闰年的处理方法
to_char( last_day( to_date('02':year,'mmyyyy') ), 'dd' )
如果是28就不是闰年

小提示:内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。

数据库时间 推荐文章:
推荐不满意?点这里  ››  

数据库时间