473,225 Members | 1,264 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Set time for UTC

Hi,

I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.

So for example the time is 7pm.

If this is the case I need to set the time to 5pm the next where the
final date and time will be set to a new datetime variable.

Can anyone help??

Examples would be great.

Regards
Simon

Aug 2 '06 #1
5 4130
I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.
I don't fully understand your requirements and how this problem relates to
your subject but the example below should get you started.

DECLARE
@datetime1 datetime,
@datetime2 datetime

SET @datetime1= '20060802 19:00:00'

--compare only time portion
IF CAST(CONVERT(char(8), @datetime1, 114) AS datetime) '17:00:00'
BEGIN
--after 5pm: make the time 5pm and add 2 days
SET @datetime2 = CAST(CONVERT(char(8), @datetime1, 112) + ' 17:00:00' AS
datetime) + 2
END
ELSE
BEGIN
--5pm or earlier: leave time unchanged add 2 days
SET @datetime2 = @datetime1 + 2
END
SELECT @datetime1, @datetime2

--
Hope this helps.

Dan Guzman
SQL Server MVP

<si********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi,

I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.

So for example the time is 7pm.

If this is the case I need to set the time to 5pm the next where the
final date and time will be set to a new datetime variable.

Can anyone help??

Examples would be great.

Regards
Simon

Aug 2 '06 #2
Here is what I think you are asking for. Start with a date, add 48
hours, and then if the result is later than 5pm, set the time to 5pm.

create table Dates (d datetime)

INSERT Dates values ('2006-08-02 08:45')
INSERT Dates values ('2006-08-02 18:45')
INSERT Dates values ('2006-08-02 22:45')

SELECT d,
CASE WHEN datepart(hour,d) >= 17
THEN dateadd(hour,17,dateadd(day,2 + datediff(day,0,d),0))
ELSE dateadd(day,2,d)
END as Future
FROM Dates

d Future
------------------------ -----------------------
2006-08-02 08:45:00.000 2006-08-04 08:45:00.000
2006-08-02 18:45:00.000 2006-08-04 17:00:00.000
2006-08-02 22:45:00.000 2006-08-04 17:00:00.000

Roy Harvey
Beacon Falls, CT

On 2 Aug 2006 03:21:35 -0700, si********@hotmail.com wrote:
>Hi,

I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.

So for example the time is 7pm.

If this is the case I need to set the time to 5pm the next where the
final date and time will be set to a new datetime variable.

Can anyone help??

Examples would be great.

Regards
Simon
Aug 2 '06 #3
On Wed, 02 Aug 2006 12:38:39 GMT, "Dan Guzman"
<gu******@nospam-online.sbcglobal.netwrote:
>I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.

I don't fully understand your requirements and how this problem relates to
your subject but the example below should get you started.

DECLARE
@datetime1 datetime,
@datetime2 datetime

SET @datetime1= '20060802 19:00:00'

--compare only time portion
IF CAST(CONVERT(char(8), @datetime1, 114) AS datetime) '17:00:00'
BEGIN
--after 5pm: make the time 5pm and add 2 days
SET @datetime2 = CAST(CONVERT(char(8), @datetime1, 112) + ' 17:00:00' AS
datetime) + 2
END
ELSE
BEGIN
--5pm or earlier: leave time unchanged add 2 days
SET @datetime2 = @datetime1 + 2
END
SELECT @datetime1, @datetime2
Eww, magic numbers! And isn't Roy's solution using DatePart() and
DateAdd() a lot cleaner than converting to and from char?
Aug 2 '06 #4
Eww, magic numbers! And isn't Roy's solution using DatePart() and
DateAdd() a lot cleaner than converting to and from char?
I generally prefer CONVERT rather than DATEPART/DATEADD to isolate the time
because it makes it easier to handle more complex time expressions.
DATEPART starts to get ugly when minutes and seconds need to be considered
(e.g. '17:30:45').

Whether or not one technique is cleaner than the other is fairly subjective
but the DATEPART/DATEADD method Roy suggested probably will perform better
than CONVERT.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Ed Murphy" <em*******@socal.rr.comwrote in message
news:uf********************************@4ax.com...
On Wed, 02 Aug 2006 12:38:39 GMT, "Dan Guzman"
<gu******@nospam-online.sbcglobal.netwrote:
>>I need to add 48 hours to a specific date (datetime data type) then
check if the time is past 5pm.

I don't fully understand your requirements and how this problem relates to
your subject but the example below should get you started.

DECLARE
@datetime1 datetime,
@datetime2 datetime

SET @datetime1= '20060802 19:00:00'

--compare only time portion
IF CAST(CONVERT(char(8), @datetime1, 114) AS datetime) '17:00:00'
BEGIN
--after 5pm: make the time 5pm and add 2 days
SET @datetime2 = CAST(CONVERT(char(8), @datetime1, 112) + ' 17:00:00'
AS
datetime) + 2
END
ELSE
BEGIN
--5pm or earlier: leave time unchanged add 2 days
SET @datetime2 = @datetime1 + 2
END
SELECT @datetime1, @datetime2

Eww, magic numbers! And isn't Roy's solution using DatePart() and
DateAdd() a lot cleaner than converting to and from char?

Aug 3 '06 #5
On Thu, 03 Aug 2006 01:24:48 GMT, "Dan Guzman"
<gu******@nospam-online.sbcglobal.netwrote:
>Whether or not one technique is cleaner than the other is fairly subjective
but the DATEPART/DATEADD method Roy suggested probably will perform better
than CONVERT.
I always used to strip off the time by converting to a character
string. I only learned the DATEADD/DATEDIFF approach recently from
reading the SQL Server newsgroups. I am told it is far more
efficient, but I am still trying to decide if it is readable. But I
have had to create some real messes with character strings and
DATEPART and such too.

I still haven't really accepted the simple date arithmatic: SET
@datetime2 = @datetime1 + 2 from your example. I know it works in
days, but I like the explicit unit DAY in DATEADD. I don't even
bother to use the abbreviations in DATEADD, DATEPART or DATENAME any
more, as I find the full word more explicit.

Roy
Aug 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
1
by: Alfonso Morra | last post by:
Hi I'm compiling some code and need to generate some random numbers. To save time, I decided to use the srand, rand and time functions. My code worked (atleast built fine) until I added time.h,...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Roseanne | last post by:
We are experiencing very slow response time in our web app. We run IIS 6 - windows 2003. I ran iisstate. Here's what I got. Any ideas?? Opened log file 'F:\iisstate\output\IISState-812.log'...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.