当前位置:服务支持 >  软件文章 >  Sybase日期函数应用示例 业务场景实用代码

Sybase日期函数应用示例 业务场景实用代码

阅读数 8
点赞 0
article_banner

Sybase日期函数是我们经常会使用的函数,下面为您介绍的几个Sybase日期函数应用例子都是我们常用的功能,供您参考学习。

Sybase日期函数
--------------------------------------------------------------------------------

日期函数
getdate()
得到当前时间,可以设置得到各种时间格式.
datepart(日期部分,日期)
取指定时间的某一个部分,年月天时分秒.
datediff(日期部分,日期1,日期2)
计算指定的日期1和日期2的时间差多少.
dateadd(日期部分,数值表达式,日期)
计算指定时间,再加上表达式指定的时间长度.

--取时间的某一个部分

select datepart(yy,getdate()) --year  
select datepart(mm,getdate()) --month  
select datepart(dd,getdate()) --day  
select datepart(hh,getdate()) --hour  
select datepart(mi,getdate()) --min  
select datepart(ss,getdate()) --sec  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

--取星期几

set datefirst 1  
select datepart(weekday,getdate()) --weekday  
 
  • 1.
  • 2.
  • 3.

--字符串时间

select getdate() -- '03/11/12'  
select convert(char,getdate(),101) -- '09/27/2003'  
select convert(char,getdate(),102) -- '2003.11.12'  
select convert(char,getdate(),103) -- '27/09/2003'  
select convert(char,getdate(),104) -- '27.09.2003'  
select convert(char,getdate(),105) -- '27-09-2003'  
select convert(char,getdate(),106) -- '27 Sep 2003'  
select convert(char,getdate(),107) --'Sep 27, 2003'  
select convert(char,getdate(),108) --'11:16:06'  
select convert(char,getdate(),109) --'Sep 27 2003 11:16:28:746AM'  
select convert(char,getdate(),110) --'09-27-2003'  
select convert(char,getdate(),111) --'2003/09/27'  
select convert(char,getdate(),112) --'20030927'  
select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2003.11.12 11:03:41'  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

--整数时间

select convert(int,convert(char(10),getdate(),112)) -- 20031112  
select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646  
 
  • 1.
  • 2.
  • 3.

--时间格式 "YYYY.MM.DD HH:MI:SS" 转换为 "YYYYMMDDHHMISS"

declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)  
select @a=convert(datetime,'2004.08.03 12:12:12')  
select @tmp=convert(char(10),@a,112)  
select @tmp  
select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))  
select @tmp1  
select @tmp=@tmp+@tmp1  
select @tmp  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

--当月***一天

declare  
@tmpstr varchar(10)  
@mm int,  
@premm int,  
@curmmlastday varchar(10)  
begin  
select @mm=datepart(month,getdate())--当月  
select @premm=datepart(month,dateadd(month,-1,getdate())) --上个月  
if (@mm>=1 and @mm<=8)  
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'  
else if (@mm>=9 and @mm<=11)  
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'  
else  
select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'  
select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) --当月***一天  
end  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空