我希望创建一个存储过程,可以检索小于或大于当前sys日期的日期时间..在我的表中,startdate和enddate的值为'datetime'
如何在SQL存储过程中获取startdate和enddate之间的详细信息?
提前致谢
答案 0 :(得分: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
答案 1 :(得分:3)
例如:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
在3天内获得
答案 2 :(得分:2)
听起来您正试图根据日期范围过滤表格中的数据。如果是这种情况(我在理解你的问题时遇到一些麻烦),你会做这样的事情:
select *
from MyTable m
where m.Date between @DateFrom and @DateTo
现在,我假设您的过滤日期已放入变量@DateFrom
和@DateTo
。
答案 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);