检查T-SQL中两个日期之间的日期

时间:2022-04-13 08:08:42

I have a stored procedure where want check that a date is between a fixed date and the current date/time (with GETDATE()):

我有一个存储过程,要检查日期是在固定日期和当前日期/时间之间(使用GETDATE()):

SELECT
    a, b
FROM myTbl
WHERE
    DATE BETWEEN 'Day start datetime' AND GETDATE()

...for example :

...例如 :

WHERE
    DATE BETWEEN '2013-09-10 00:00:00.00' AND 'GETDATE()'

How to do it?

怎么做?

4 个解决方案

#1


14  

A pair of DATEADD/DATEDIFF calls will round a date down to the previous midnight:

一对DATEADD / DATEDIFF调用会将日期向下舍入到上一个午夜:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and GETDATE()

Alternatively, if you're on SQL Server 2008 or later:

或者,如果您使用的是SQL Server 2008或更高版本:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN CONVERT(date,GETDATE()) and GETDATE()

#2


6  

'GETDATE()' is a string literal, GETDATE() is a T-SQL function.

'GETDATE()'是一个字符串文字,GETDATE()是一个T-SQL函数。

Your query should look like:

您的查询应如下所示:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN '2013-09-10 00:00:00.0' and GETDATE()

#3


1  

I think WHERE DATE BETWEEN '2013-09-10 00:00:00.00' and GETDATE() (without the single quotes around the GETDATE() call) should work just fine.

我想在'2013-09-10 00:00:00.00'和GETDATE()之间的日期(没有围绕GETDATE()调用的单引号)应该可以正常工作。

#4


0  

You can get Day start date time by converting Getdate() return value to a Date type and again to Datetime as below.

您可以通过将Getdate()返回值转换为Date类型并再次转换为Datetime来获取Day开始日期时间,如下所示。

select a,b
from myTbl
where [date] between convert(datetime,convert([date], getdate())) 
                 and getdate()

#1


14  

A pair of DATEADD/DATEDIFF calls will round a date down to the previous midnight:

一对DATEADD / DATEDIFF调用会将日期向下舍入到上一个午夜:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and GETDATE()

Alternatively, if you're on SQL Server 2008 or later:

或者,如果您使用的是SQL Server 2008或更高版本:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN CONVERT(date,GETDATE()) and GETDATE()

#2


6  

'GETDATE()' is a string literal, GETDATE() is a T-SQL function.

'GETDATE()'是一个字符串文字,GETDATE()是一个T-SQL函数。

Your query should look like:

您的查询应如下所示:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN '2013-09-10 00:00:00.0' and GETDATE()

#3


1  

I think WHERE DATE BETWEEN '2013-09-10 00:00:00.00' and GETDATE() (without the single quotes around the GETDATE() call) should work just fine.

我想在'2013-09-10 00:00:00.00'和GETDATE()之间的日期(没有围绕GETDATE()调用的单引号)应该可以正常工作。

#4


0  

You can get Day start date time by converting Getdate() return value to a Date type and again to Datetime as below.

您可以通过将Getdate()返回值转换为Date类型并再次转换为Datetime来获取Day开始日期时间,如下所示。

select a,b
from myTbl
where [date] between convert(datetime,convert([date], getdate())) 
                 and getdate()