2010-11-24 99 views
1

我希望创建一个存储过程,它可以检索小于或大于当前sys日期的日期时间..在我的表中,startdate和enddate的值为'datetime'获取日期和时间的SQL存储过程

如何在SQL存储过程中获取startdate和enddate之间的详细信息?

在此先感谢

+1

有无限多的日期和时间比SYSDATE更小或更大。我不太清楚你想要做什么。你能多解释一下吗? – 2010-11-24 09:10:43

+0

您正在使用哪个数据库? – 2010-11-24 09:15:21

+0

嗨iam使用两个日期.. startdate和enddate定义为smalldatetime – Ganesh 2010-11-24 13:16:43

回答

3

考虑到这个表定义

CREATE TABLE [dbo].[Dates](
    [StartDate] [datetime] NOT NULL, 
    [EndDate] [datetime] NOT NULL 
) 

我认为如果你传递一个日期,你想知道哪些行满足条件:的startDate <日期<结束日期。如果是这种情况下,你可以使用查询:

select * 
from Dates 
where convert(datetime, '20/12/2010', 103) between StartDate and EndDate; 

存储过程可能看起来像:

ALTER PROCEDURE [dbo].[GetDataWithinRange] 
    @p_Date datetime 
AS 
BEGIN 
    SELECT * 
    from Dates 
    where @p_Date between StartDate and EndDate; 
END 

2

这听起来像你想的基础上的日期范围表中筛选数据。如果是这样的话(我有一些无法理解你的问题),你会做这样的事情:

select * 
from  MyTable m 
where  m.Date between @DateFrom and @DateTo 

现在,我假设你的过滤日期放入变量@DateFrom@DateTo

3

如:

SELECT * 
FROM MyTable 
WHERE DATEDIFF ('d',mydatefield ,getdate()) < 3 

得在3天内

0
There are two things: 

1> To get todays date we can write 
SET @today_date = GETTDDT(); 

2> To get Current time we can us ethe following query: 

SET @today_time = (SELECT            
       digits(cast(hour(current time) as decimal(2,0)))|| 
       digits(cast(minute(current time) as decimal(2,0)))|| 
       digits(cast(second(current time) as decimal(2,0))) 
       FROM sysibm/sysdummy1);