SYBASE时间计算
以摘录了计算月初,月末,上月同日,下月同日,上年同日,下年同日(年有闰月问题),各种函数输出格式。 可以写到存储过程中也可单独使用。
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时间计算的更多相关文章
- C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期
C# 时间计算 今天.昨天.前天.明天 class Program { static void Main(string[] args) { ...
- ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
- Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)
uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...
- STM32的定时器定时时间计算(计数时间和中断定时时间)
时基单元 可编程高级控制定时器的主要部分是一个16位计数器和与其相关的自动装载寄存器.这个计数器可以向上计数.向下计数或者向上向下双向计数.此计数器时钟由预分频器分频得到. 计数器.自动装载寄存器和预 ...
- c++时间计算模块
c++时间计算模块 可用于计算代码运行耗时.计算代码运行时间线(比如处理与运行时间相关函数). 该模块从实际项目中产生,使用方式仁者见仁智者见智,设计思想可供参考. 源码: //author: cai ...
- Mysql学习笔记—时间计算、年份差、月份差、天数差(转载)
1.获取当前日期 SELECT NOW(),CURDATE(),CURTIME(); 结果类似: 2. 获取前一天 DAY); 当前日期2018-09-17,结果: 3. 获取后一天 DAY); 当前 ...
- php 当前时间计算操作
首先要设置时间为中国时区 date_default_timezone_set('PRC'); 对于获取当前时间戳后的各种时间计算 数据库保存最好用时间戳 当前时间time() 上一天 echo dat ...
- Executors线程池关闭时间计算
Executors线程池关闭时间计算 学习了:http://blog.csdn.net/wo541075754/article/details/51564359 https://www.cnblogs ...
- C# TimeSpan 时间计算
原文:C# TimeSpan 时间计算 本文告诉大家简单的方法进行时间计算. 实际上使用 TimeSpan 可以做到让代码比较好懂,而代码很简单. 例如我使用下面的代码表示 5 秒 const int ...
随机推荐
- [改善Java代码]构造函数尽量简化
建议34: 构造函数尽量简化 我们知道在通过new关键字生成对象时必然会调用构造函数,构造函数的简繁情况会直接影响实例对象的创建是否繁琐.在项目开发中,我们一般都会制订构造函数尽量简单,尽可能不抛异常 ...
- Oracle导入SQL脚本执行 scott 用户下的表删除了
执行 .sql 文件时,应在 sqlplus 或 cmd 中执行,速度比plsql 中的command window 中书许多, scott 用户下的表删除了 可以执行如下 @D:\app\Admi ...
- hihocoder 1237 Farthest Point
#1237 : Farthest Point 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 Given a circle on a two-dimentional pla ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- linux 查看端口是否被占用
查看端口是否被占用: netstat -anp | grep port lsof -i:port 查看端口被那个进程占用: netstat -anp | grep port 或使用 lsof -i:p ...
- Jackson - Features for configuring Java-to-JSON mapping
Following on/off features are defined in SerializationConfig.Feature (for Jackson 1.x), or Serializa ...
- HttpClient(4.3.5) - HTTP Execution Context
Originally HTTP has been designed as a stateless, response-request oriented protocol. However, real ...
- UML-用例
- IOS 下拉菜单
由于之前曾经用到过下拉菜单,所以现在花一些时间回过头来细细整理了一下,逐步完善这个下拉菜单,并提供一些比较基本的功能,以便日后如果有需要的话可以进行复用,并提供给需要的人参考.下拉菜单同样分为数据源和 ...
- 学习之spring属性文件注入
package com.my.proper; import org.springframework.beans.factory.annotation.Value; import org.springf ...