在T-SQL中计算日期差异

在T-SQL中计算日期差异,sql,sql-server,tsql,Sql,Sql Server,Tsql,伙计们,我正在尝试用T-SQL(SQL Server)编写一个存储过程,它将根据日期字段选择记录,并记住分钟的差异。大概是这样的: CREATE PROCEDURE spGetCustomers(@DateRange DATETIME, @Variance int) AS -- The next line is where I need help -- I'm trying to subtract X amount of minutes from the date -- So if @Varia

伙计们,我正在尝试用T-SQL(SQL Server)编写一个存储过程,它将根据日期字段选择记录,并记住分钟的差异。大概是这样的:

CREATE PROCEDURE spGetCustomers(@DateRange DATETIME, @Variance int) AS
-- The next line is where I need help
-- I'm trying to subtract X amount of minutes from the date
-- So if @Variance = 4 AND @DateRange = '6/10/2009 1:15pm'
-- Then @StartDate should equal '6/10/2009 1:11pm'
DECLARE @StartDate = @DateRange - @Variance
-- I also need an @EndDate, which will be X amount of minutes
-- in the future. So if @Variance = 4 AND @DateRange = '6/10/2009 1:15pm'
-- Then @EndDate should equal '6/10/2009 1:19pm'
DECLARE @EndDate = @DateRange + @Variance

SELECT * FROM Customers WHERE Created BETWEEN @StartDate AND @EndDate
希望这是有意义的,有人可以帮助我!提前感谢

查看以下内容:

DATEADD函数允许您将日期的几乎任何部分添加到另一个日期对象,它应该是您需要的一切

因此,基本上:

SELECT DATEADD(second, @Variance, @DateRange)
看看这个:

DATEADD函数允许您将日期的几乎任何部分添加到另一个日期对象,它应该是您需要的一切

因此,基本上:

SELECT DATEADD(second, @Variance, @DateRange)

下面的脚本提供了一个示例,可以帮助您入门

create table tmp_Customers
(
    ID int identity(1,1),
    CreatedDate datetime default getDate() not null,
    Description varchar(15)
);
go

insert into tmp_Customers(Description) values('SomeData');
insert into tmp_Customers(Description) values('SomeData2');
insert into tmp_Customers(Description) values('SomeData3');
go

create procedure usp_GetCustomers

    @iVarianceMinutes   int,
    @iDateRange         datetime

as

    set nocount on

    declare @startDate  datetime
    declare @endDate    datetime

    --Define the date ranges for the select query
    set @startDate = dateAdd(minute,-@iVarianceMinutes,@iDateRange)
    set @endDate = dateAdd(minute,@iVarianceMinutes,@iDateRange)

    --Get the Customers that were created within this time range.
    SELECT * 
    FROM tmp_Customers 
    WHERE CreatedDate >= @startDate and CreatedDate < @endDate 


return(0);
go


--Execute the procedure
declare @testDate datetime;
set @testDate = getDate();

exec usp_GetCustomers 5,@testDate 

--drop procedure usp_GetCustomers
--drop table tmp_Customers
创建tmp\U客户表
(
ID int标识(1,1),
CreatedDate datetime默认getDate()不为空,
说明varchar(15)
);
去
在tmp_客户(描述)中插入值(“SomeData”);
在tmp_客户(描述)中插入值(“SomeData2”);
在tmp_客户(描述)中插入值(“SomeData3”);
去
创建usp_GetCustomers程序
@iVarianceMinutes int,
@iDateRange日期时间
作为
不计较
声明@startDate datetime
声明@endDate-datetime
--定义select查询的日期范围
设置@startDate=dateAdd(分钟、@iVarianceMinutes、@iDateRange)
设置@endDate=dateAdd(分钟、@iVarianceMinutes、@iDateRange)
--获取在此时间范围内创建的客户。
选择*
来自tmp_客户
其中CreatedDate>=@startDate和CreatedDate<@endDate
返回(0);
去
--执行程序
声明@testDate-datetime;
设置@testDate=getDate();
执行usp_GetCustomers 5,@testDate
--投递程序usp\U GetCustomers
--投递台tmp_客户

下面的脚本提供了一个示例,可以帮助您入门

create table tmp_Customers
(
    ID int identity(1,1),
    CreatedDate datetime default getDate() not null,
    Description varchar(15)
);
go

insert into tmp_Customers(Description) values('SomeData');
insert into tmp_Customers(Description) values('SomeData2');
insert into tmp_Customers(Description) values('SomeData3');
go

create procedure usp_GetCustomers

    @iVarianceMinutes   int,
    @iDateRange         datetime

as

    set nocount on

    declare @startDate  datetime
    declare @endDate    datetime

    --Define the date ranges for the select query
    set @startDate = dateAdd(minute,-@iVarianceMinutes,@iDateRange)
    set @endDate = dateAdd(minute,@iVarianceMinutes,@iDateRange)

    --Get the Customers that were created within this time range.
    SELECT * 
    FROM tmp_Customers 
    WHERE CreatedDate >= @startDate and CreatedDate < @endDate 


return(0);
go


--Execute the procedure
declare @testDate datetime;
set @testDate = getDate();

exec usp_GetCustomers 5,@testDate 

--drop procedure usp_GetCustomers
--drop table tmp_Customers
创建tmp\U客户表
(
ID int标识(1,1),
CreatedDate datetime默认getDate()不为空,
说明varchar(15)
);
去
在tmp_客户(描述)中插入值(“SomeData”);
在tmp_客户(描述)中插入值(“SomeData2”);
在tmp_客户(描述)中插入值(“SomeData3”);
去
创建usp_GetCustomers程序
@iVarianceMinutes int,
@iDateRange日期时间
作为
不计较
声明@startDate datetime
声明@endDate-datetime
--定义select查询的日期范围
设置@startDate=dateAdd(分钟、@iVarianceMinutes、@iDateRange)
设置@endDate=dateAdd(分钟、@iVarianceMinutes、@iDateRange)
--获取在此时间范围内创建的客户。
选择*
来自tmp_客户
其中CreatedDate>=@startDate和CreatedDate<@endDate
返回(0);
去
--执行程序
声明@testDate-datetime;
设置@testDate=getDate();
执行usp_GetCustomers 5,@testDate
--投递程序usp\U GetCustomers
--投递台tmp_客户