Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
milliseconds in datetime variable.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Martin  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 06:41
Newsgroups: microsoft.public.sqlserver.programming
From: "Martin" <martin_remove_and_no_spam@martinz_remove_.co.nz>
Date: Mon, 9 Nov 2009 19:41:11 +1300
Local: Mon 9 Nov 2009 06:41
Subject: milliseconds in datetime variable.

Hi,

I am wondering how to compare 2 datetime values so that milliseconds are
taken into account.

as an example I have the following script.

it appears that if I compare two datetime variables then milliseconds are
not taken into account, however if I compare two datatime2 variables then
milliseconds are taken into account.

I have put together the script below to demonstate the problem.

any help is appreciated.

thanks in advance.

cheers

martin.

USE [tempdb];
GO
declare @Date1 datetime = '2009-07-16 22:31:54.416';
declare @Date2 datetime = '2009-07-16 22:31:54.417';

declare @Date3 datetime2 = '2009-07-16 22:31:54.416';
declare @Date4 datetime2 = '2009-07-16 22:31:54.417';

/*Try comparing datetime datatype*/
print 'Compare datetime to datetime - Incorrect';
if @Date1 < @Date2
        print 'Correct Answer'
Else
        print 'Incorrect Answer'

/*Try comparing datetime2 datatype*/
print 'Compare datetime2 to datetime2 - correct';
if @Date3 < @Date4
        print 'Correct Answer'
Else
        print 'Incorrect Answer'

/*Try comparing datetime and datetime2 datatype*/
print 'Compare datetime2 to datetime2 - incorrect';
if @Date1 < @Date4
        print 'Correct Answer'
Else
        print 'Incorrect Answer'

print CONVERT(datetime2, @date1, 113); /*This will round incorrectly*/
print CONVERT(datetime2, @Date2, 113);
print @date3;
print @date4;
GO


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sylvain Lafontaine  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 07:08
Newsgroups: microsoft.public.sqlserver.programming
From: "Sylvain Lafontaine" <sylvainlafontaine2...@yahoo.ca>
Date: Mon, 9 Nov 2009 02:08:10 -0500
Local: Mon 9 Nov 2009 07:08
Subject: Re: milliseconds in datetime variable.
The precision of the datetime values is a little bigger then 3 milliseconds
(it's not exactly 3 milliseconds) so you cannot compare them with a greater
precision then around 3 milliseconds:

select convert (datetime, '2009-07-16 22:31:54.002)
select convert (datetime, '2009-07-16 22:31:54.016)
select convert (datetime, '2009-07-16 22:31:54.017)

The precision for datetime2 is higher than that but I don't remember by how
much.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)

"Martin" <martin_remove_and_no_spam@martinz_remove_.co.nz> wrote in message

news:6A6A3B67-1152-4DCD-9155-7D01A9BCB6CD@microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Cooper  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 08:36
Newsgroups: microsoft.public.sqlserver.programming
From: "Tom Cooper" <tomcoo...@comcast.net>
Date: Mon, 9 Nov 2009 03:36:05 -0500
Local: Mon 9 Nov 2009 08:36
Subject: Re: milliseconds in datetime variable.
As Sylvain noted, the accuracy of datetime is about 3 milliseconds.
Specificly, if you have a time of
hh:mm:ss.ttt
where ttt is the milliseconds value, when you store it in a datetime column
or variable, the last digit in ttt is rounded to 0, 3, or 7 before storing
it in the variable or column.  So when you store '2009-07-16 22:31:54.416',
it is converted to '2009-07-16 22:31:54.417'.  Which is why your comparisons
return a difference of 0 milliseconds.

Datetime2 variables and columns have a precision of 100 nanoseconds.  So if
you need an accuracy better than + or - 3 milliseconds or so, you need to
use datetime2.

Tom

"Sylvain Lafontaine" <sylvainlafontaine2...@yahoo.ca> wrote in message

news:u45SttQYKHA.5368@TK2MSFTNGP02.phx.gbl...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vern Rabe  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 18:12
Newsgroups: microsoft.public.sqlserver.programming
From: Vern Rabe <VernR...@discussions.microsoft.com>
Date: Mon, 9 Nov 2009 10:12:02 -0800
Local: Mon 9 Nov 2009 18:12
Subject: RE: milliseconds in datetime variable.
Although the datetime datatype displays down to 1 millisecond, its accuracy
is only to about 3 milliseconds. The result is that although you are
assigning a value of '2009-07-16 22:31:54.416' to a variable of datetime
type, it is really getting assigned '2009-07-16 22:31:54.417'. You can see
this by:

DECLARE @dt datetime;
SET @dt = '2009-07-16 22:31:54.414';
SELECT @dt;
SET @dt = '2009-07-16 22:31:54.415';
SELECT @dt;
SET @dt = '2009-07-16 22:31:54.416';
SELECT @dt;
SET @dt = '2009-07-16 22:31:54.417';
SELECT @dt;
SET @dt = '2009-07-16 22:31:54.418';
SELECT @dt;
SET @dt = '2009-07-16 22:31:54.419';
SELECT @dt;

The solution is to use datetime2

HTH
Vern Rabe


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Henrik Staun Poulsen  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 19:14
Newsgroups: microsoft.public.sqlserver.programming
From: Henrik Staun Poulsen <h...@stovi.com>
Date: Tue, 10 Nov 2009 11:14:58 -0800 (PST)
Local: Tues 10 Nov 2009 19:14
Subject: Re: milliseconds in datetime variable.
Hi Martin,

The solution is to use datetime2(3)

This takes up 7 bytes, which is one by less than the old DateTime data
type.
(who cares about 1 byte per row? I do; our largest table has 65
billion rows. 65 GB extra does not cost a lot, but I/O time is
important)

Best regards,
Henrik

On Nov 9, 7:41 am, "Martin"


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google