Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Between Dynamic Dates

Author  Topic 

ralphceo
Starting Member

2 Posts

Posted - 2005-02-23 : 11:01:16
Hello,

To determine if the current date is between Monday 6AM and Friday 5PM you may want to consider the following T-SQL code.

DECLARE @FromDay int, @ToDay int
DECLARE @FromTime varchar(10), @ToTime varchar(10)
DECLARE @Day int
DECLARE @RangeFrom datetime, @RangeTo datetime

SET @Day = DatePart (dw, getDate())
SET @FromDay = 2
SET @ToDay = 6
SET @FromTime = '06:00'
SET @ToTime = '17:00'

IF (@Day >= @FromDay) AND (@Day <= @ToDay) BEGIN
PRINT 'Day ' + CAST(@Day AS varchar)
PRINT 'FromDay ' + CAST(@FromDay as varchar)
PRINT 'ToDay ' + cast(@ToDay as varchar)
SET @RangeFrom = CONVERT(varchar, GetDate() - (@Day - @FromDay), 110) + ' ' + @FromTime
SET @RangeTo = CONVERT(varchar, GetDate() + (@ToDay - @Day), 110) + ' ' + @ToTime
PRINT @RangeFrom
PRINT GetDate()
PRINT @RangeTo
IF (GetDate() >= @RangeFrom AND GetDate() <= @RangeTo) BEGIN
PRINT 'Between Dates'
END
END

clarkbaker1964
Constraint Violating Yak Guru

428 Posts

Posted - 2005-02-23 : 13:31:22
Hi... Why dont you put it in a function that returns scaler value or recordset so it can be used in any sql statement

You can do anything at www.zombo.com
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2005-02-25 : 12:39:02
Moved to the Script Library.

===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-10-06 : 00:50:20
[code]
select
DT,
[Is Between Dates?] =
case
when DT between
-- Monday at 06:00
dateadd(dd,(datediff(dd,'17530101',DT)/7)*7,'17530101 06:00') and
-- Friday at 17:00
dateadd(dd,(datediff(dd,'17530105',DT)/7)*7,'17530105 17:00')
then 'Between Dates'
else 'Not Between Dates'
end
from
(Select DT = getdate() ) a
[/code]
Results:
[code]
DT Is Between Dates?
-------------------------- -----------------
2006-10-06 00:46:14.493 Between Dates

(1 row(s) affected)


[/code]

CODO ERGO SUM
Go to Top of Page
   

- Advertisement -