SQL的日期问题,怎样提取表中的日期,懂的帮我下~!

SQL的日期问题,怎样提取表中的日期,懂的帮我下~!,第1张

首先指出的是,在查询的时候计算并不是一个好的解决办法,应该是在数据表里增加一个“有效期”字段,当产生一条借书登记时,根据借书时间自动生成一条一个月后的有效期时间。这样非常有助于筛选显示。

其次查询的方法是:select , dateadd(MONTH,1,借书时间) from 借书记录表,你的“select dateadd(MONTH,1,日期)”已经是正确的,只需要将你的日期修改成对应你数据表里的借书登记时间即可。

这个委实有点难度,看你是什么数据库了。

如果不指定数据库的话,作为通用解决方案:

可以考虑 用一个日历表 ,每天一条记录,这样 你从这个表中 就可以 查询出来了。

create procedure p_date @begindate datetime,@enddate datetime

as

begin

declare @diff int,@i int

select @i=0

select @diff=datediff(day,@begindate,@enddate)

create table #temp (dt datetime primary key(dt))

while @i<=@diff

begin

insert into #temp values (dateadd(day,@i,@begindate))

select @i=@i+1

end

select from #temp

drop table #temp

end

调用方式:

exec p_date '2009-9-1','2009-9-9'

这个是个实现方式,但是具体你要怎么个用法还需要自己去调整

--获取日期字段的年

select to_char(sysdate,'yyyy') as year from dual

或者:(指定日期)

select to_char(to_date('2013/08/08','yyyy/mm/dd'),'yyyy') as year from dual

--获取日期字段的月

select to_char(sysdate,'mm') as month from dual

--获取日期字段的日

select to_char(sysdate,'dd') as day  from dua

部分效果如下:

select from 表 where 日期字段>='开始日期' and 日期字段<='截止日期'  and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间'。

SELECT FROM 表明 WHERE 日期字段名 BETWEEN '20130101' AND '20130130'。

例如:

select from tb1 where dDate>='2010-11-05' and dDate<='2010-11-15' 

and convert(char(8),dDate,108)>='8:00:00' and convert(char(8),dDate,108)<='9:00:00'

select from table1 where year(d)=2010 and month(d)=7 and day(d) between 1 and 31

and (Datepart(hour,d)>=22 or Datepart(hour,d)<6)

扩展资料:

SQL查询日期:

今天的所有数据:select from 表名 where DateDiff(dd,datetime类型字段,getdate())=0

昨天的所有数据:select from 表名 where DateDiff(dd,datetime类型字段,getdate())=1

7天内的所有数据:select from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7

30天内的所有数据:select from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30

本月的所有数据:select from 表名 where DateDiff(mm,datetime类型字段,getdate())=0

本年的所有数据:select from 表名 where DateDiff(yy,datetime类型字段,getdate())=0

参考资料:

SQL_百度百科

以上就是关于SQL的日期问题,怎样提取表中的日期,懂的帮我下~!全部的内容,包括:SQL的日期问题,怎样提取表中的日期,懂的帮我下~!、怎么用SQL语句把两个日期间的所有日期列出一个列表、SQL语句实现一段时间内的每一个日期的列表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9380886.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存