Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case Statment with GETDATE()

Status
Not open for further replies.

nutrotek

Technical User
Aug 10, 2006
8
US
I'm trying to write a CASE statement that evaluates if GETDATE() > Todays Date at 12pm. If true I want to use tomorrows date at 12pm, if false use todays date at 5 pm.

This is what I came up with:

CASE WHEN (CAST(GETDATE() AS DECIMAL (24,2))) > (CAST(GETDATE() AS DECIMAL (24,0)) + .5)
THEN (CAST(GETDATE() AS DECIMAL (24,2)) + 1.5) ELSE (CAST(GETDATE() AS DECIMAL (24,2)) + .7)
END

Not working. Where am I going wrong?
 
This may not be EXACTLY what you want, but it should be pretty close.

Code:
Select Case When DatePart(Hour, '20060810 [!]10:00:00 AM[/!]') > 12
            Then DateAdd(Hour, 12, DateAdd(Day, 1, DateDiff(Day, 0, GetDate())))
            Else DateAdd(Hour, 17, DateAdd(Day, 0, DateDiff(Day, 0, GetDate())))
            End

Change the part in red to test the functionality.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Here's a slight alternative, using the current date and time:
Code:
SELECT CASE
	WHEN DATEPART(hour, GETDATE()) > 12 THEN
		DATEADD(dd, 2, DATEADD(hh, -12 - DATEPART(hh, GETDATE()), DATEADD(mi, -DATEPART(mi, GETDATE()), DATEADD(ss, -DATEPART(ss, GETDATE()), DATEADD(ms, -DATEPART(ms, GETDATE()), GETDATE())))))
	ELSE
		DATEADD(dd, 1, DATEADD(hh, -7 - DATEPART(hh, GETDATE()), DATEADD(mi, -DATEPART(mi, GETDATE()), DATEADD(ss, -DATEPART(ss, GETDATE()), DATEADD(ms, -DATEPART(ms, GETDATE()), GETDATE())))))
	END

Sorry for reposting, I had been working on it and been away from my desk, so I thought I'd add it as well, perhaps it will help.

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top