Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to find Date by a given hour in relative to current date time

Posted on 2011-03-22
6
Medium Priority
?
237 Views
Last Modified: 2012-06-27
Hi Experts

this is a little bit complicated to me:

i get a hour (for example 22:00)
i should find the last date of this  hour
and the next date of this hour
and of course i have the getdate() to use it.

so actually i need 2 dates: previous date and next date

10x a lot
0
Comment
Question by:gudidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 35190712
please give an example, it is not clear what you want to do.
0
 

Author Comment

by:gudidi
ID: 35190780
ok

i have from database the hour 22:00.

i need to calculate the datetime of last time that the hour was 22:00
and date time of next occurance of 22:00 hour.

for example:

if the time now is 18:00
the last  datetime of 22:00 was 21/03/2011
na dnext time is 22/03/2011
0
 
LVL 2

Accepted Solution

by:
CoolBurn28 earned 2000 total points
ID: 35190955
hehehe nice
this would do the trick
select getdate() sysdate,DATEADD(hh, 22, getdate()) nxt_22,DATEADD(hh, -22, getdate()) prev_22

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:gudidi
ID: 35190997
10x so much
0
 
LVL 18

Expert Comment

by:deighton
ID: 35191109
DECLARE @HOUR as integer;
SET @HOUR = 20;
DECLARE @YEST as datetime;
DECLARE @TODAY as datetime;
DECLARE @TOMORROW as datetime;
DECLARE @BASE as datetime;
SET @BASE = CAST(FLOOR(CAST(getdate() AS FLOAT)) as datetime);
SET @YEST = dateadd(hour, @HOUR, dateadd(day,-1,@BASE));
SET @TODAY = dateadd(day, 1, @yest);
SET @TOMORROW=dateadd(day, 2, @yest);

SELECT CASE WHEN @TODAY <= getdate() THEN  @TODAY ELSE @YEST END AS LASTTIME,
	   CASE WHEN @TODAY >= getdate() THEN  @TODAY ELSE @TOMORROW END AS NEXTTIME

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 35191133
the code you accepted gives the time 22 hours ago and 22 hours into the future, but it doesn't give you the time it was last 22 hours and the time it is next 22 hours

but if that is what you wanted so-be-it
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question