开发者

ASP.Net SQL Where Date is after Today

开发者 https://www.devze.com 2023-02-21 23:11 出处:网络
Hi all I have the following Query made using the Query Builder in Visual Studio. SELECT Schd_ID, Schd_Date, Schd_Avaliable, Sch开发者_C百科d_Nights, Schd_Price, Accom_ID

Hi all I have the following Query made using the Query Builder in Visual Studio.

SELECT Schd_ID, Schd_Date, Schd_Avaliable, Sch开发者_C百科d_Nights, Schd_Price, Accom_ID
FROM   Schedule
WHERE  (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID)

I want to add another WHERE statement which adds where Schd_Date is after todays date, any ideas?


Assuming SQL Server, the GETDATE() function returns the date and time the statement was run at:

WHERE Schd_Date > GETDATE()

Use the following for finding dates greater than the current date at midnight:

WHERE Schd_Date > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

However, CURRENT_TIMESTAMP is the ANSI means of getting the current date and time in a database. Beyond that, date functionality is not consistent between databases so you'd have to tell us what you are dealing with for better answers.


If this is SQL Server you could use the GETDATE() function to return the current date and compare against it:

SELECT 
    Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID 
FROM 
    Schedule 
WHERE 
    (Schd_Avaliable = 'Yes') 
AND 
    (Accom_ID = Accom_ID)
AND 
    (Schd_Date > GETDATE())


SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM   Schedule
WHERE  (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND (GETDATE() < Schd_Date)

This should work


I guess this expression can be used as a date representing the current date on the format yyyy-mm-dd (without the hours,minutes and seconds).

(Datepart('yyyy',getdate())+ '-' +Datepart('m',getdate())+ '-' + Datepart('d',getdate()))

therefore

SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID 
FROM   Schedule    
WHERE  (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND 
       (Schd_Date >= (Datepart('yyyy',getdate())+ '-' + 
                     Datepart('m',getdate())+ '-' + 
                     Datepart('d',getdate())))
0

精彩评论

暂无评论...
验证码 换一张
取 消