数据库的时间运算

oracle

--取现在时间
select sysdate from dual  --2010-7-15 10:51:31
--前两天,默认以天为单位
select sysdate-2 from dual  --2010-7-13 10:51:31
--前一小时
select sysdate-1/24 from dual  --2010-7-15 9:51:31
--前两个月
select add_months(sysdate,-2) from dual  --2010-5-15 10:51:31

--字符串转日期
select to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss') from dual  --2010-7-15 10:51:31
select to_date('2010-7-15','yyyy-mm-dd') from dual  --2010-7-15

--日期转字符串
select to_char(sysdate,'yyyy') from dual  --2010
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'yyyy') from dual  --2010
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'year') from dual  --twenty ten
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'mm') from dual --07
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'month') from dual --7月
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'dd') from dual  --15
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'day') from dual  --星期四
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'hh24') from dual  --10
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'mi') from dual  --51
select to_char(to_date('2010-7-15 10:51:31','yyyy-mm-dd hh24:mi:ss'),'ss') from dual  --31

--求时间差
select to_date('2010-07-14 9:12:35','yyyy-mm-dd hh24:mi:ss') -
to_date('2010-07-13 15:12:35','yyyy-mm-dd hh24:mi:ss') from dual  --0.75,默认以天为单位
select (to_date('2010-07-14 9:12:35','yyyy-mm-dd hh24:mi:ss') -
to_date('2010-07-13 15:12:35','yyyy-mm-dd hh24:mi:ss'))*24 from dual  --18,转化为小时
select (to_date('2010-07-14 9:12:35','yyyy-mm-dd hh24:mi:ss') -
to_date('2010-07-13 15:12:36','yyyy-mm-dd hh24:mi:ss'))*24 from dual  --17.9997222222222,与分秒有关

--选出2010-7-13 15:30:00之后的新闻
select NewsId,NewsTitle from BS_News where CreateTime >= to_date('2010-7-13 15:30:00','yyyy-mm-dd hh24:mi:ss')
--选出2010-7-13 00:00:00之后的新闻
select NewsId,NewsTitle  from BS_News where CreateTime >= to_date('2010-7-13 00:00:00','yyyy-mm-dd hh24:mi:ss')
--等价于:select NewsId,NewsTitle  from BS_News where CreateTime >= to_date('2010-7-13','yyyy-mm-dd')
--选出2010.07.12的新闻
select NewsId,NewsTitle  from BS_News where CreateTime between to_date('2010-07-12','yyyy-mm-dd') and to_date('2010-07-12

23:59:59','yyyy-mm-dd hh24:mi:ss')

 

sql

--取现在时间
select getdate() --2010-07-13 10:51:31.793
--前两天
select dateadd(dd,-2,getdate())  --2010-07-13 10:51:31.793
--前一小时
select dateadd(hh,-1,getdate())  --2010-07-15 9:51:31.793
--前两个月
select dateadd(mm,-2,getdate())  --2010-05-15 10:51:31.793

--字符串转日期
select cast('2010-07-15 10:51:31.793' as datetime)  --2010-07-15 10:51:31.793

--日期转字符串
select cast(getdate() as varchar(18))  --07 15 2010  10:51AM
select convert(varchar(18),getdate())   --07 15 2010  10:51AM
--转成更多字符串格式
Select convert(varchar(19),getdate(), 20)  --2010-07-15 10:51:31
Select convert(varchar(10),getdate(), 111)  --2010/07/15

--求时间差
--精确到年份,2010-2009=1,与月/日无关
select datediff(yyyy, '2009-07-14', '2010-01-20') 
--精确到小时,11-9+24=26,与分/秒无关
select datediff(hh,'2010-07-14 9:12:35.012','2010-07-15 11:50:35.012') 
--类似的date-part单位还有mm-月,dd-日,mi-分钟,ss-秒,mi-毫秒

--选出2010-7-13 15:30:00之后的新闻
select NewsId,NewsTitle from BS_News where CreateTime >= cast('2010-7-13 15:30:00' as datetime)
--选出2010-7-13 00:00:00之后的新闻
select NewsId,NewsTitle from BS_News where CreateTime >= cast('2010-7-13 00:00:00' as datetime)
--等价于:select NewsId,NewsTitle from BS_News where CreateTime >= cast('2010-7-13' as datetime)
--选出2010.07.12的新闻
select NewsId,NewsTitle from BS_News where CreateTime between cast('2010-07-12' as datetime) and cast('2010-07-12 23:59:59'

as datetime)

转载于:https://www.cnblogs.com/vipcjob/archive/2010/07/27/1778080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值