[英]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.