SQl语句 日期相加

作者&投稿:古响 (若有异议请与网页底部的电邮联系)
oracle时间相加问题,比如我想在一个时间上加上3个小时 ,sql语句怎么写啊~

select sysdate,to_char(sysdate+3/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加3个小时

--------------其他的:
select sysdate,add_months(sysdate,12) from dual; --加1年
select sysdate,add_months(sysdate,1) from dual; --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒

在sql server里可以使用:
where start_date <=
DateAdd(d,1,to_date('2005-12-09','yyyy-mm-dd'))
and completion_date >=
to_date('2005-12-09', 'yyyy-mm-dd') ;

oracle中没有定义和sql server中一样的DateAdd函数,
oracle可以通过interval 'n' year/month/day/hour/minute/second/second(p,s)
的方式来增减时间
下面是自己在oracle中写的DateAdd函数
函数调用基本同sql server一样, 不过datepart部分需要以字符串的方式输入, 即
DateAdd(d,1,to_date('2005-12-09','yyyy-mm-dd'))
要改为
DateAdd('d',1,to_date('2005-12-09','yyyy-mm-dd'))

函数定义如下函数中的注释是datepart的新说明, 与sql server中的略有不同)
create or replace function DATEADD( datepart varchar2, num number, indate date ) return date is
Result date;
v_sql varchar2(1000);
v_datepart varchar2(30);
v_ms varchar2(13);
begin
v_datepart := lower(datepart);
/*
Datepart Abbreviations
year yy, y
quarter qq, q
month mm, m
day dd, d
week wk, w
hour hh, h
minute mi, n
second ss, s
millisecond ms
*/
case
when v_datepart in ('year','yy','y') then
v_sql := 'select :1 + interval '''||num||''' year from dual';
when v_datepart in ('quarter','qq','q') then
v_sql := 'select :1 + (interval ''3'' month) * '||num||' from dual';
when v_datepart in ('month','mm','m') then
v_sql := 'select :1 + interval '''||num||''' month from dual';
when v_datepart in ('week','wk','w') then
v_sql := 'select :1 + (interval ''7'' day) * '||num||' from dual';
when v_datepart in ('day','dd','d') then
v_sql := 'select :1 + interval '''||num||''' day from dual';
when v_datepart in ('hour','hh') then
v_sql := 'select :1 + interval '''||num||''' hour from dual';
when v_datepart in ('minute','mi','n') then
v_sql := 'select :1 + interval '''||num||''' minute from dual';
when v_datepart in ('second','ss','s') then
v_sql := 'select :1 + interval '''||num||''' second from dual';
when v_datepart in ('millisecond','ms') then
v_ms := to_char(num/1000,'fm999999990.000');
v_sql := 'select :1 + interval '''||v_ms||''' second(9,3) from dual';
else
RAISE_APPLICATION_ERROR(-20001, ''''||datepart||''' is not a recognized dateadd option.' );
end case;

execute immediate v_sql into Result using indate;

return(Result);

EXCEPTION
WHEN OTHERS THEN
RAISE ;

end DATEADD;

access与SQL SERVER的日期函数有一点区别,
如果是access的话:
就用:dateadd("d",5,#2008-08-08#)
如果是SQL SERVER的话:
dateadd(d,5,"2008-08-08")
呵呵,希望能有帮助,^_^

SqlStr="select * from 表 where 时间字段=dateadd(d,5,'2008-08-08')"

汗..楼上的..有这么复杂吗?

select dateadd(day,5,'2008-08-08')

就OK了..不用转成date型了

dateadd(d,5,"2008-08-08")

select dateadd(d,5,cast('2008-08-08'as datetime))
select cast('2008-08-08'as datetime)

2008-08-13 00:00:00.000
2008-08-08 00:00:00.000

MSsqlServer 2000测试通过


康县19241531043: SQL中怎么实现时间相加.比如 我要实现 从今天的日期加30天为到期日. -
凌卞亚思: 是日期型的直接+30 是int类型的要加3600*24*30

康县19241531043: 如何用SQL对相同的日期记录进行求和 -
凌卞亚思: select username,convert(varchar(10),[datetime],120),sum(number) from [table] group by username,convert(varchar(10),[datetime],120)

康县19241531043: SQL语句 怎样比较两个日期的大小,简单一点的 -
凌卞亚思: 惯例,在等号左边尽量不要有对字段的运算,所以一般用法有: 1、判断其是否在某个日期区间: Where CheckDate Between '2013-01-01' And '2013-01-31' 这个方法也可用于加几天是多少,或减几天是多少: 把起迄日期参数化,原...

康县19241531043: sql server 日期天数加减语句怎么写 -
凌卞亚思: select dateadd( day, 10 ,'2016-01-01' ) 结果为:'2016-01-11'

康县19241531043: 关于数据表中时间对应的数据相加的SQL语句该怎么写! -
凌卞亚思: sqlserver下 每周: select * from 表 where date between cast('2013-11-07' as datetime)-7 and cast('2013-11-07' as datetime) 每月: select * from 表 where date between cast('2013-11-07' as datetime)-30 and cast('2013-11-07' as datetime);

康县19241531043: 怎样让SQL的一条记录自动加上日期? -
凌卞亚思: 有几种方法 1,设计默认值为getdate()这样插入新数据的时候如果没有指定time的时自动插入当前时间2.sql语句里面使用getdate()

康县19241531043: 对时间列求和的sql语句怎么写 -
凌卞亚思: select year(trandate) * 100 + month(trandate) as trandate, sum(tranamount) as tranamount from normaltran group by year(trandate) * 100 + month(trandate) 结果: 200801 140 200802 45 如果一定要 2008-01这种格式也可以,作相应的就该就行. cast(year(trandate) as varchar(5)) + '-' + cast(month(trandate) as varchar(2))!

康县19241531043: 怎样将日期增加2年,用SQL语言 -
凌卞亚思: DateAdd ( I , N , D ) 将一个日期加上一段期间后的日期 I :设定一个日期( Date )所加上的一段期间的单位.譬如 interval="d" 表示 N的单位为日. I的设定值如下: yyyy Year 年 q Quarter 季 m Month 月 d Day 日 w Weekday 星期 h Hour 时 n ...

康县19241531043: sql中,一个时间字段+一个整数字段形成一个新的时间,求SQL语句!!!!!! -
凌卞亚思: DATEADD(year,10,Date):加10年 DATEADD(month,10,Date):加10月 DATEADD(day,10,Date):加10天

康县19241531043: SQL 两个时间字段相加 -
凌卞亚思: 如果类型为CHAR(5),结果仍为CHAR(5) 那么这样写 select newtime= substring( convert(varchar,convert(datetime,time1,120)+convert(datetime,time2,120),120),12,5)

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网