如何使用sql查找日期之间? [英] How to find a between dates using sql?
问题描述
我正在预订房间预订网站上工作。我收到了一个错误,错误是我从日期和日期开始选择日期和日期并将预订详细信息提交到数据库..
该值将保存为此类型..
起始日期:2014年8月25日
到目前为止: 29/08/2014
问题:当我预订其他预订详情时,我不希望日期为2014年8月25日至2014年8月29日。日期之间没有保存在database.only从日期和日期保存到database.how阻止日期之间使用sql ...
Am working on a reservation room ticket booking website.i got a error,the error was i have a from date and to date when i select from date and to date and submit the reservation details into the data base..
the value will saved into this type..
From date : 25/08/2014
To date : 29/08/2014
Problem: when i book another reservation details i dont want the date between 25/08/2014 to 29/08/2014. the between dates are not saved in database.only the from date and to date are saving into the database.how to block between date using sql...
推荐答案
1 )首先,您可以在SQL Server中找到日期和日期之间的所有日期,例如
1)First you can find all the dates between from and to dates in SQL Server like
Create PROCEDURE getAllDaysBetweenTwoDate
(
@FromDate DATETIME,
@ToDate DATETIME
)
AS
BEGIN
DECLARE @TOTALCount INT
SET @FromDate = DATEADD(DAY,-1,@FromDate)
Select @TOTALCount= DATEDIFF(DD,@FromDate,@ToDate);
WITH d AS
(
SELECT top (@TOTALCount) AllDays = DATEADD(DAY, ROW_NUMBER()
OVER (ORDER BY object_id), REPLACE(@FromDate,'-',''))
FROM sys.all_objects
)
SELECT AllDays From d
RETURN
END
GO
然后将所选日期与结果集进行比较。
2)At代码背后:
获取from和to循环之间的所有日期,然后与所选日期进行比较。
then compare selected date with the result set.
2)At Code Behind:
Get the all dates between from and to dates through loop then compare with selected date.
这篇关于如何使用sql查找日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!