2010-11-24 47 views
1

我希望創建一個存儲過程,它可以檢索小於或大於當前sys日期的日期時間..在我的表中,startdate和enddate的值爲'datetime'獲取日期和時間的SQL存儲過程

如何在SQL存儲過程中獲取startdate和enddate之間的詳細信息?

在此先感謝

+1

有無限多的日期和時間比SYSDATE更小或更大。我不太清楚你想要做什麼。你能多解釋一下嗎? – 2010-11-24 09:10:43

+0

您正在使用哪個數據庫? – 2010-11-24 09:15:21

+0

嗨iam使用兩個日期.. startdate和enddate定義爲smalldatetime – Ganesh 2010-11-24 13:16:43

回答

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 

2

這聽起來像你想的基礎上的日期範圍表中篩選數據。如果是這樣的話(我有一些無法理解你的問題),你會做這樣的事情:

select * 
from  MyTable m 
where  m.Date between @DateFrom and @DateTo 

現在,我假設你的過濾日期放入變量@DateFrom@DateTo

3

如:

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

得在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);