= [ClosedFrom] AND <= [ClosedTo]" />
开发者

SQL Query - Check if date is on a closed day

I am creating an online booking system & need to exclude closed days from being booked & need a select query to see if the "date" (Christmas day) is >= [ClosedFrom] AND <= [ClosedTo]

Both ClosedFrom & ClosedTo are datetime columns in SQL Server 2008.

Here is what my table look like:

 ----------------------------------------------------------
 ClosedDateID     --     ClosedFrom     --     ClosedTo
 ----------------------------------------------------------
 1                --     2010-12-24     --     2010-01-04
 2                --     2011-04-20     --     2010-04-20
 ----------------------------------------------------------

The query below will hopefully show you what I am trying to achieve:

 SELECT [ClosedFrom],[ClosedTo]
 FROM [Package.Closed.Dates]
 where '2010-12-25' >=  [ClosedFrom] AND '2010-12-25' <= [ClosedTo] 

Thanks in advance for your help ;)


UPDATE:

Thanks Refilter for your answer but I still cannot get this to work :( I have include some more details below.

Here is my table script:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Package.Closed.Dates](
    [ClosedDateID] [int] IDENTITY(1,1) NOT NULL,
    [ClosedDate] [datetime] NULL,
    [ClosedFrom] [datetime] NULL,
    [ClosedTo] [datetime] NULL,
 CONSTRAINT [PK_Package.Closed.Dates] PRIMARY KEY CLUSTERED 
(
    [ClosedDateID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Here is my table data:

INSERT INTO [dbo].[Package.Closed.Dates]
           ([ClosedDate]
           ,[ClosedFrom]
           ,[ClosedTo])
     VALUES
           ('2010-12-25'
           ,'2010-12-20'
           ,'2010-12-28'
GO

Here is my query to that checks a single date (ClosedDate) to see if we are closed (THIS RETURNS "we are CLOSED that day" - WORKING):

if exists (

SELECT [ClosedDate]
  FROM [Package.Closed.Dates]
  where [ClosedDate] =  '2010-12-25'

)

select 'we are CLOSED that day'

else 

select 'we are OPEN that day'

Here is your query to that checks between two dates (ClosedFrom & C开发者_运维技巧losedTo) to see if we are closed (THIS ALWAYS RETURNS "we are OPEN that day" - NOT WORKING):

if exists (

SELECT [ClosedFrom],[ClosedTo] 
  FROM [Package.Closed.Dates]
where '2010-12-25' between [ClosedFrom] AND [ClosedTo]  

)

select 'we are CLOSED that day'

else 

select 'we are OPEN that day'

I have also tried the below query (THIS ALWAYS RETURNS "we are OPEN that day" - NOT WORKING):


if exists (
SELECT [ClosedFrom],[ClosedTo] 
  FROM [MWD.Package.Closed.Dates]
where '2010-12-25' >= [ClosedFrom] AND '2010-12-25' <= [ClosedTo]

) select 'we are CLOSED that day' else select 'we are OPEN that day'

Just one other quick note, I need to see if the "date" (Christmas day) is >= [ClosedFrom] AND <= [ClosedTo]


SELECT [ClosedFrom], [ClosedTo]  
FROM [Package.Closed.Dates]  
where '2010-12-25' between [ClosedFrom] AND [ClosedTo]  


This might work for you. This removes time component and makes it 00

DECLARE @DateToCheck DATETIME
SET @DateToCheck = '2010-12-25'    

IF EXISTS ( SELECT 1   
            FROM [Package.Closed.Dates]   
            WHERE CONVERT(DATETIME,@DateToCheck,112) BETWEEN CONVERT(DATETIME, 
                  [ClosedFrom],112) AND CONVERT(DATETIME,[ClosedTo] ,112)
          )
   SELECT 'we are CLOSED that day'    
ELSE     
   SELECT 'we are OPEN that day'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜