1
votes

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'

How do I get details between startdate and enddate in SQL stored procedure?

thanks in advance

4
There are infinitely many dates and times that are less than or greater than sysdate. I'm not quite sure what you are trying to do. Can you explain a bit more?Martin Schapendonk
Which Database are you using?Adriaan Stander
hi iam using two dates.. startdate and enddate defined as smalldatetimethe_dopamine
The database seems like SQL Server. What do you mean by "date and time are as separate fields"? Can you post the structure of your table?sh_kamalh
hi kamal i have updated the questionthe_dopamine

4 Answers

3
votes

eg:

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

gets within 3 days

3
votes

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. 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
2
votes

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.

0
votes
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);