[英]SQL Stored Procedure to get Date and Time
我希望创建一个存储过程,该存储过程可以检索小于或大于当前系统日期的日期时间。在我的表中,开始日期和结束日期的值为“ datetime”
如何在SQL存储过程的开始日期和结束日期之间获取详细信息?
提前致谢
考虑此表定义
CREATE TABLE [dbo].[Dates](
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
我假设如果您传递一个日期,您想知道哪些行满足条件:startDate <date <EndDate。 在这种情况下,您可以使用查询:
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
例如:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
在3天内得到
听起来您正在尝试根据日期范围过滤表中的数据。 如果是这种情况(我在理解您的问题时遇到了一些麻烦),您将执行以下操作:
select *
from MyTable m
where m.Date between @DateFrom and @DateTo
现在,我假设您的过滤日期已放入变量@DateFrom
和@DateTo
。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.