查询数据库当天某时间段数据

本文介绍了如何使用SQL在不同的数据库管理系统(MSSQL、MYSQL、Oracle、Access)中查询当天的数据记录,并提供了具体语法示例。此外,还展示了如何查询特定时间段内的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、SQL在查询当天记录时要注意是从当天的0点0分0秒0毫秒开始,到次日0点0分0秒0毫秒截止,但不包含次日的0点0分0秒0毫秒。

2、注意:在不同数据库产品中,获得当天日期的函数不一样。

    MSSQL获得当前日期:convert(varchar(10),Getdate(),120)

    MYSQL获得当前日期:date(now())
    Oracle获得当前日期:to_char(sysdate,'yyyy-mm-dd')

    Access获得当前日期:date()

3、在各个数据库里获得当天的记录写法为(假设表名为:Table_1,日期列名为:date_col):

    MSSQL获得当天记录:

1
select * from table_1 where date_col>=convert(varchar(10),Getdate(),120) and date_col<convert(varchar(10),dateadd(d,1,Getdate()),120)
    MYSQL获得当天记录:
1
select * from table_1 where date_col>=date(now()) and date_col<DATE_ADD(date(now()),INTERVAL 1 DAY)
    Oracle获得当天记录:

1
select * from table_1 where date_col>=to_char(sysdate,'yyyy-mm-dd') and date_col<to_char(sysdate+1,'yyyy-mm-dd')
    Access获得当天记录:

1
select * from table_1 where date_col>=date() and date_col<DateAdd("d",1,date())


如果查询固定时间段的数据:
"select * from biao where (here>=date(now()) and here<DATE_ADD(date(now()),INTERVAL 1 DAY)) and time(here) between time('8:00:00') and time(now()) order by here;"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值