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.
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 intDECLARE @FromTime varchar(10), @ToTime varchar(10)DECLARE @Day intDECLARE @RangeFrom datetime, @RangeTo datetimeSET @Day = DatePart (dw, getDate())SET @FromDay = 2SET @ToDay = 6SET @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' ENDEND |
|
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 statementYou can do anything at www.zombo.com |
|
|
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. |
|
|
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' endfrom (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 |
|
|
|
|
|
|
|