星空网 > 软件开发 > 数据库

SQL获取前一天0:00:00至23:59:59数据

一、前言

因为我公司要做财务结算前一天0:00:00至23:59:59的数据,利用到动态拼接SQL语句

我们需要明白声明DateTime 和 Date 获取的时间格式是不一样的,所以通过此计算有利于得到我们需要的时间段数据

二、测试数据展示(自己看print结果)

DECLARE @startDate DATE;DECLARE @startDateTime DATETIME;DECLARE @endDateTime DATETIME ;SET @startDate=GETDATE();SELECT @startDateTime=DATEADD(DAY,-1,@startDate); SET @endDateTime=DATEADD(second,-1,CONVERT(DATETIME,@startDate))print(@startDateTime)print(CONVERT(DATETIME,@startDate))print(@startDate)print(@endDateTime)

 

SQL获取前一天0:00:00至23:59:59数据

三、动态SQL语句

DECLARE @startDate DATE;DECLARE @startDateTime DATETIME;DECLARE @endDateTime DATETIME ;SET @startDate=GETDATE();SELECT @startDateTime=DATEADD(DAY,-1,@startDate); SET @endDateTime=DATEADD(second,-1,CONVERT(DATETIME,@startDate))declare @sql nvarchar(1000)set @sql='select * from E_OrdersLogistics where ForecastTime>='''+cast(ISNULL(@startDateTime,'') as nvarchar(100)) +''' and ForecastTime<'''+CAST(ISNULL(@endDateTime,'') as nvarchar(100))+''''print(@sql)exec(@sql)

SQL获取前一天0:00:00至23:59:59数据

如需转载,请注明出处,谢谢大家支持~

 




原标题:SQL获取前一天0:00:00至23:59:59数据

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流