How to Search date in SQL?

被刻印的时光 ゝ 提交于 2020-01-04 01:49:10

问题


I am having an Event Management System in which i want,

If an event is registered for 5 days (21 jan 2009 to 26 Jan 2009) Then if another person wants to register an event between 22 jan 2009 to 24 jan 2009 then it will not allow to register. I want to check this using SQL query, so please tell me how can i do this.


回答1:


SELECT *
FROM events e
WHERE (@startDate BETWEEN e.startDate and e.endDate)
OR (@endDate BETWEEN e.startDate and e.endDate)
OR (@startDate < e.startDate AND @endDate > e.endDate)




回答2:


Just a to complete other answers, you have a good article on How to Search for Date and Time Values Using SQL Server 2000

It reminds you about how date/time values are stored (two date/time data types: datetime and smalldatetime)

It also points out Datetime and smalldatetime are like the floating-point data types, float and real, in that they’re approximate numerics. That means the value retrieved from SQL Server may be different from the value that was originally stored.

Plus, it warns about Database designers who don’t always use date/time columns appropriately. At the time the database is designed, each date/time column should be identified as to whether it will store both dates and times, dates only, or times only.

It closes with practical queries on data/time.

You also have a good description of DATEADD and DATEDIFF here.




回答3:


Lookup DATEDIFF in SQL help.



来源:https://stackoverflow.com/questions/358630/how-to-search-date-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!