以摘录了计算月初,月末,上月同日,下月同日,上年同日,下年同日(年有闰月问题),各种函数输出格式。 可以写到存储过程中也可单独使用。
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,因其计算出的星期天为一周的第1天,星期六自然就成为一周第7天
set datefirst 1
select datepart(weekday,getdate()) --weekday
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'
--整数时间
select convert(int,convert(char(10),getdate(),112)) --
20031112
select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 +
datepart(ss,getdate()) -- 110646
--时间格式 "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
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
----------------------------------------------------------
格式为:DateAdd(interval, number, date)
interval:间隔时间(如时、分、秒、天、周、月、季、年)
number:间隔数(正数为向后+N,负数为向前-N)
date:当前日期或指定日期
查询当前日期起,以时、分、秒、天、周、月、季、年为间隔的日期
时:select convert(char(8),dateadd(hour,1,getdate()),112)
分:select convert(char(8),dateadd(minute,1,getdate()),112)
秒:select convert(char(8),dateadd(second,1,getdate()),112)
天:select convert(char(8),dateadd(day,1,getdate()),112)
周:select convert(char(8),dateadd(week,1,getdate()),112)
月:select convert(char(8),dateadd(month,1,getdate()),112)
季:select convert(char(8),dateadd(quarter,1,getdate()),112)
年:select convert(char(8),dateadd(year,1,getdate()),112)
select dateadd(year, 1, '31-Jan-96')
select dateadd( month, 1, '1987/05/02' )
select datediff(day,'2008-10-1','2008-10-31')
上月第一天:
select
dateadd(mm,-1,convert(char(8),(datepart(yy,getdate())*10000+datepart(mm,getdate())*100+01)))
select
dateadd(dd,-1,convert(char(8),(datepart(yy,getdate())*10000+datepart(mm,getdate())*100+01)))
上年最后一天:
select convert(char(8), dateadd(dd, -1,
convert(char(8),  datepart(yy,
'20100223')*10000+1*100+01, 112)), 112)
select
convert(char(8),dateadd(yy,-1,convert(char(8),@report_date,112)),112)

SYBASE时间计算的更多相关文章

  1. C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期

    C# 时间计算    今天.昨天.前天.明天   class Program    {        static void Main(string[] args)        {          ...

  2. ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间

    ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间

  3. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...

  4. STM32的定时器定时时间计算(计数时间和中断定时时间)

    时基单元 可编程高级控制定时器的主要部分是一个16位计数器和与其相关的自动装载寄存器.这个计数器可以向上计数.向下计数或者向上向下双向计数.此计数器时钟由预分频器分频得到. 计数器.自动装载寄存器和预 ...

  5. c++时间计算模块

    c++时间计算模块 可用于计算代码运行耗时.计算代码运行时间线(比如处理与运行时间相关函数). 该模块从实际项目中产生,使用方式仁者见仁智者见智,设计思想可供参考. 源码: //author: cai ...

  6. Mysql学习笔记—时间计算、年份差、月份差、天数差(转载)

    1.获取当前日期 SELECT NOW(),CURDATE(),CURTIME(); 结果类似: 2. 获取前一天 DAY); 当前日期2018-09-17,结果: 3. 获取后一天 DAY); 当前 ...

  7. php 当前时间计算操作

    首先要设置时间为中国时区 date_default_timezone_set('PRC'); 对于获取当前时间戳后的各种时间计算 数据库保存最好用时间戳 当前时间time() 上一天 echo dat ...

  8. Executors线程池关闭时间计算

    Executors线程池关闭时间计算 学习了:http://blog.csdn.net/wo541075754/article/details/51564359 https://www.cnblogs ...

  9. C# TimeSpan 时间计算

    原文:C# TimeSpan 时间计算 本文告诉大家简单的方法进行时间计算. 实际上使用 TimeSpan 可以做到让代码比较好懂,而代码很简单. 例如我使用下面的代码表示 5 秒 const int ...

随机推荐

  1. jQuery扩展工具方法

    共享学习Jquery源码的一些东西 jQuery.extend({   expando  :  生成唯一JQ字符串(内部) noConflict()  :  防止冲突 ---------------- ...

  2. 如果将WCF服务发布为rest模式

    WCF是支持多种协议的,其中basicHttpBinding是基础协议绑定,类似于传统的webservice. 如果要将WCF发布成rest,绑定协议要使用webHttpBinding,并且在终结点的 ...

  3. 关于eclipse中egit右键reset失败,无法更新git仓库.

    有时候egit出现莫名其妙的问题,这次是无法对git仓库进行reset . & & 解决方法: 找到对应git仓库的磁盘目录,然后进行git brash.(一定要安装了git客户端) ...

  4. 【XMLRPC实现跨语言编程】Tcl <----> python

    转载请声明出处,谢谢合作. # 期望一种能实现tcl.python两者解释器能双向通讯的结合体# py->tcl: from Tkinter import Tcl; tcl = Tcl(); t ...

  5. C++STL学习笔记_(4)queue

    10.2.5Queue容器 Queue简介 ²  queue是队列容器,是一种"先进先出"的容器. ²  queue是简单地装饰deque容器而成为另外的一种容器. ²  #inc ...

  6. sublime安装 less环境

    工具的选择: mac-codekit simpless->跨平台 winless-windows  less.js下载:http://pan.baidu.com/s/1o60yTZ0   安装L ...

  7. Ajax--跨域访问的三种方法

    一.什么是跨域 我们先回顾一下域名地址的组成: / script/jquery.js  http:// (协议号) www  (子域名) google (主域名) (端口号) script/jquer ...

  8. Java Concurrency - Phaser, Controlling phase change in concurrent phased tasks

    The Phaser class provides a method that is executed each time the phaser changes the phase. It's the ...

  9. Jersey(1.19.1) - Hello World, Get started with a Web application

    1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...

  10. pdf文件流生成pdf文件

    protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...