![SQL获取前一天0:00:00至23:59:59数据 SQL获取前一天0:00:00至23:59:59数据](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
一、前言
因为我公司要做财务结算前一天0:00:00至23:59:59的数据,利用到动态拼接SQL语句
我们需要明白声明DateTime 和 Date 获取的时间格式是不一样的,所以通过此计算有利于得到我们需要的时间段数据
二、测试数据展示(自己看print结果)
DECLARE @startDate DATE; DECLARE @startDateTime DATETIME; DECLARE @endDateTime DATETIME ; SET @startDate=GETDATE(); ,@startDate); ,CONVERT(DATETIME,@startDate)) print(@startDateTime) print(CONVERT(DATETIME,@startDate)) print(@startDate) print(@endDateTime)
三、动态SQL语句
DECLARE @startDate DATE; DECLARE @startDateTime DATETIME; DECLARE @endDateTime DATETIME ; SET @startDate=GETDATE(); ,@startDate); ,CONVERT(DATETIME,@startDate)) ) )) ))+'''' print(@sql) exec(@sql)