获取日期和时间的SQL存储过程
我希望创建一个存储过程,它可以检索小于或大于当前系统日期的日期时间。。在我的表中,startdate和enddate的值为“datetime” 如何在SQL存储过程中获取startdate和enddate之间的详细信息获取日期和时间的SQL存储过程,sql,sql-server,Sql,Sql Server,我希望创建一个存储过程,它可以检索小于或大于当前系统日期的日期时间。。在我的表中,startdate和enddate的值为“datetime” 如何在SQL存储过程中获取startdate和enddate之间的详细信息 考虑到此表定义,请提前感谢 CREATE TABLE [dbo].[Dates]( [StartDate] [datetime] NOT NULL, [EndDate] [datetime] NOT NULL ) 我假设如果您传递一个日期,您希望知道哪些行满足
考虑到此表定义,请提前感谢
CREATE TABLE [dbo].[Dates](
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
我假设如果您传递一个日期,您希望知道哪些行满足条件:startDateselect *
from Dates
where convert(datetime, '20/12/2010', 103) between StartDate and EndDate;
考虑到此表定义,存储过程可能如下所示:
CREATE TABLE [dbo].[Dates](
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
ALTER PROCEDURE [dbo].[GetDataWithinRange]
@p_Date datetime
AS
BEGIN
SELECT *
from Dates
where @p_Date between StartDate and EndDate;
END
我假设如果您传递一个日期,您希望知道哪些行满足条件:startDateselect *
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 m
where m.Date between @DateFrom and @DateTo
现在,我假设您的筛选日期被放入变量
@DateFrom
和@DateTo
中。听起来您正试图根据日期范围筛选表中的数据。如果是这种情况(我在理解你的问题时遇到一些困难),你可以这样做:
select *
from MyTable m
where m.Date between @DateFrom and @DateTo
现在,我假设您的筛选日期被放入变量@DateFrom
和@DateTo
中,例如:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
选择*
从MyTable
其中DATEDIFF('d',mydatefield,getdate())<3
在3天内获取,例如:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
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);
选择*
从MyTable
其中DATEDIFF('d',mydatefield,getdate())<3
在3天内获取有无限多个日期和时间小于或大于sysdate。我不太清楚你想做什么。你能再解释一下吗?你用的是哪个数据库?嗨,我用了两个日期。。startdate和enddate定义为SmallDateTime该数据库类似于SQL Server。“日期和时间作为单独的字段”是什么意思?你能发布你的表格结构吗?嗨,卡马尔,我已经更新了这个问题,有无限多的日期和时间小于或大于sysdate。我不太清楚你想做什么。你能再解释一下吗?你用的是哪个数据库?嗨,我用了两个日期。。startdate和enddate定义为SmallDateTime该数据库类似于SQL Server。“日期和时间作为单独的字段”是什么意思?你能发布你的表格结构吗?嗨,卡马尔,我已经更新了-3和-3之间的问题where datediff('d',mydatefield,getdate())where datediff('d',mydatefield,getdate())where datediff('d',mydatefield,getdate())where datediff('d',mydatefield,getdate())在-3和3did之间,在发布答案之前。我不想在我的上网本上启动SQL server。我想我已经做了足够多的这种类型的查询,我不必为了给出与问题相关的答案而进行测试。在发布答案之前,你测试过答案吗?没有。我不想在我的上网本上启动SQL server。我想我做这种类型的查询已经足够了,我不必为了给出一个与问题相关的答案而测试它。
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);