SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 Between Dynamic Dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ralphceo
Starting Member

2 Posts

Posted - 02/23/2005 :  11:01:16  Show Profile  Reply with Quote
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

USA
428 Posts

Posted - 02/23/2005 :  13:31:22  Show Profile  Visit clarkbaker1964's Homepage  Reply with Quote
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

USA
4137 Posts

Posted - 02/25/2005 :  12:39:02  Show Profile  Visit graz's Homepage  Reply with Quote
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)

USA
7020 Posts

Posted - 10/06/2006 :  00:50:20  Show Profile  Reply with Quote

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

Results:

DT                         Is Between Dates?
-------------------------- ----------------- 
2006-10-06 00:46:14.493    Between Dates

(1 row(s) affected)




CODO ERGO SUM
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000