从两个日期变量中的表中检索记录

时间:2021-09-09 21:29:59

I have a table which I want to query records from based on a timescale between two dates.

我有一个表,我想根据两个日期之间的时间尺度来查询记录。

I have two variables,

我有两个变量,

DECLARE @StartDate datetime;
DECLARE @EndDate datetime;

What clause could I add to only show the records found within dates applied to these two variables?

我可以添加什么子句只显示在应用于这两个变量的日期中找到的记录?

This is based on a 'Date' column within the table.

这基于表格中的“日期”列。

2 个解决方案

#1


3  

SELECT myColumn
  FROM myTable
 WHERE Date BETWEEN @StartDate AND @EndDate

Edited: Between clause is inclusive (both dates are included in the result) so if you maybe want to exclude one of the dates in the variable columns better use:

编辑:Between子句是包含的(两个日期都包含在结果中)所以如果你想要排除变量列中的一个日期,最好使用:

SELECT myColumn
  FROM myTable
 WHERE Date >= @StartDate
   AND Date <= @EndDate

#2


2  

SELECT * FROM [your_table] WHERE Date>=@StartDate AND Date<=@EndDate

#1


3  

SELECT myColumn
  FROM myTable
 WHERE Date BETWEEN @StartDate AND @EndDate

Edited: Between clause is inclusive (both dates are included in the result) so if you maybe want to exclude one of the dates in the variable columns better use:

编辑:Between子句是包含的(两个日期都包含在结果中)所以如果你想要排除变量列中的一个日期,最好使用:

SELECT myColumn
  FROM myTable
 WHERE Date >= @StartDate
   AND Date <= @EndDate

#2


2  

SELECT * FROM [your_table] WHERE Date>=@StartDate AND Date<=@EndDate