Sybase日期函数操作

测试时的当前时间为:2010-03-23 11:10:55:955 星期二


--日期相关函数
--1.day(date_time),返回一个整数,表示指定日期的datepart中的日子。month(date_time),year(date_time)类似
select day(getdate()) --返回23
--2.datepart(datepart,date),返回类型datetime、smalldatetime、date 或 time 值(例如,月份)中以整数表示的部分
select datepart(yy,getdate()) --返回日期分量年2010
select datepart(MM,getdate()) --返回日期分量月3
select datepart(dd,getdate()) --返回日期分量天23
select datepart(hh,getdate()) --返回日期分量时11
select datepart(mi,getdate()) --返回日期分量分钟10
select datepart(ss,getdate()) --返回日期分量秒55
select datepart(ms,getdate()) --返回日期分量毫秒955
select datepart(wd,getdate()) --返回日期分量星期3,从星期日算起
select datepart(dy,getdate()) --返回日期分量一年中的第几天82,从星期日算起
--3.dateadd(datepart,number,date),返回通过向其它日期添加日期分量所产生的日期
select dateadd(dd,5,getdate())
--在当前时间的日期分量——天分量上加5天(若要减5天则写为-5,其它日期分量类似),返回2010-03-28 11:10:55:955
--4.datename(datepart,date),返回类型datetime、smalldatetime、date 或 time 值中以 ASCII 字符串表示的部分
select datename(dw,getdate()) --返回Tuesday
--5.datediff(datepart,startdate,enddate),返回enddate-startdate所在日期分量的的相差值,startdate-enddate为负值
select datediff(hh,'2010-03-23 11:10:55:955','2010-03-23 12:05:55:955')
--返回0,因为还没相差一个小时

--由当前时间获得所在年的年初
select convert(datetime,convert(varchar(4),datepart(yy,getdate()))+'0101 00:00:00')
--由当前时间获得所在月的月初
select convert(datetime,convert(varchar(12),dateadd(dd,-datepart(dd,getdate())+1,getdate()),112)+' 00:00:00')
select convert(datetime,convert(varchar(12),dateadd(dd,-day(getdate())+1,getdate()),112)+' 00:00:00')

--由当前时间获得上周时间段,如当前时间20100323 11:10:55,将得到 20100315 00:00:00 至 20100322 00:00:00 ,时分秒默认为0
select convert(datetime,convert(varchar(12),dateadd(dd,-(datepart(dw,getdate())+5), getdate()),112))
select convert(datetime,convert(varchar(12),dateadd(dd,-(datepart(dw,getdate())-2), getdate()),112))

--获得日期格式为20102303
select convert(varchar(24),getdate(),112)

--获得日期格式为2010/23/03
select convert(varchar(24),getdate(),113)

--获得日期格式为Mar 23 2010 11:11:18:346
select convert(varchar(24),getdate(),109)



一个小的应用:在SQL Advatage中计算一个语句的执行时长?


select getdate() starttime into #t_date
select '开始时间为:' + convert(varchar(24),getdate(),113) +' '+ convert(varchar(24),getdate(),108)

--在此写你的SQL,执行存储过程...

select '执行发费时长:'+case
when datediff(ss,starttime,getdate())<60 then convert(varchar(12),datediff(ms,starttime,getdate())*1.0/1000)+'秒'
when datediff(ss,starttime,getdate())>=60 and datediff(mm,starttime,getdate())<60 then convert(varchar(12),datediff(ss,starttime,getdate())*1.0/60)+'分钟'
when datediff(mm,starttime,getdate())>=60 then convert(varchar(12),datediff(mm,starttime,getdate())*1.0/60)+'小时'
end
from #t_date
drop table #t_date
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值