[英]SQL Stored Procedure to get Date and Time
I wish to create a stored procedure which can retrieve the datetime less than or greater than current sys date.. in my table,startdate and enddate has the value as 'datetime' 我希望创建一个存储过程,该存储过程可以检索小于或大于当前系统日期的日期时间。在我的表中,开始日期和结束日期的值为“ datetime”
How do I get details between startdate and enddate in SQL stored procedure? 如何在SQL存储过程的开始日期和结束日期之间获取详细信息?
thanks in advance 提前致谢
Considering this table definition 考虑此表定义
CREATE TABLE [dbo].[Dates](
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
I assume that if you pass a date you want to know which rows satisfy the condition: startDate < date < EndDate. 我假设如果您传递一个日期,您想知道哪些行满足条件:startDate <date <EndDate。 If this is the case you can use the query:
在这种情况下,您可以使用查询:
select *
from Dates
where convert(datetime, '20/12/2010', 103) between StartDate and EndDate;
A stored procedure could look like: 存储过程可能类似于:
ALTER PROCEDURE [dbo].[GetDataWithinRange]
@p_Date datetime
AS
BEGIN
SELECT *
from Dates
where @p_Date between StartDate and EndDate;
END
eg: 例如:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
gets within 3 days 在3天内得到
It sounds like you're trying to filter data in a table based on a date range. 听起来您正在尝试根据日期范围过滤表中的数据。 If this is the case (I'm having some trouble understanding your question), you'd do something like this:
如果是这种情况(我在理解您的问题时遇到了一些麻烦),您将执行以下操作:
select *
from MyTable m
where m.Date between @DateFrom and @DateTo
Now, I'm assuming your filtering dates are put into the variables @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.