473,396 Members | 1,655 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,396 software developers and data experts.

Weird date rounding

SQL Server 7.0

The following SQL:

SELECT TOP 100 PERCENT fldTSRID, fldDateEntered
FROM tblTSRs WITH (NOLOCK)
WHERE ((fldDateEntered >= CONVERT(DATETIME, '2003-11-21 00:00:00',
102))
AND
(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:59', 102)))

returns this record:

fldTSRID: 4
fldDateEntered: 24/11/2003

Hello? How is 24/11/2003 <= '2003-11-23 23:59:59'?

I tried decrementing the second predicate by seconds:

(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:30', 102)))

returns the record, but

(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:29', 102)))

does NOT.

What is happening here?

Edward

============================

TABLE DEFINITION:

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblTSRNotes_tblTSRs]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblTSRNotes] DROP CONSTRAINT FK_tblTSRNotes_tblTSRs
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblTSRs]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [dbo].[tblTSRs]
GO

CREATE TABLE [dbo].[tblTSRs] (
[fldTSRID] [int] IDENTITY (1, 1) NOT NULL ,
[fldDealerID] [int] NOT NULL ,
[fldWorkshopGroupID] [int] NULL ,
[fldSubjectID] [int] NULL ,
[fldReasonID] [int] NULL ,
[fldFaultID] [int] NULL ,
[fldContactID] [int] NULL ,
[fldMileage] [int] NULL ,
[fldFirstFailure] [smalldatetime] NULL ,
[fldNumberOfFailures] [int] NULL ,
[fldTSRPriorityID] [int] NULL ,
[fldTSRStatusID] [int] NULL ,
[fldAttachedFilePath] [char] (255) NULL ,
[fldFileAttached] [smallint] NOT NULL ,
[fldFaultDescription] [ntext] NULL ,
[fldFaultRectification] [ntext] NULL ,
[fldEmergency] [int] NOT NULL ,
[fldDateEntered] [smalldatetime] NOT NULL ,
[fldEnteredBy] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Jul 20 '05 #1
8 5444
DATETIME is only precise to 3 milliseconds, so '2003-11-23 23:59:59' rounds
to '2003-11-24 00:00:00'. Use < instead of <= with dates to avoid this sort
of problem:

SELECT fldtsrid, flddateentered
FROM tblTSRs
WHERE fldDateEntered >= '20031121'
AND fldDateEntered < '20031124'

Stick to the ISO year-month-day formats and you don't need the extra CONVERT
function in the query.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
Correction: Although my solution should be work, my explanation wasn't
accurate. It's true that the precision of DATETIME is limited to 3
milliseconds but that doesn't appear to be the root of your problem since
you haven't used milliseconds.

The precision of SMALLDATETIME is 1 minute but the value of '23:59:59' in
your query shouldn't get rounded up to the following midnight. Instead the
SMALLDATETIME value from your table should be implicitly cast to DATETIME
with no loss of precision. Your query is therefore sub-optimal since the
implicit conversion will be performed for every row. However, the query
should not produce the result you described.

I can't actually reproduce your problem. Try the query I posted previously.
If that doesn't help, please post some code to reproduce the problem,
including INSERT statement(s) with sample data.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #3
te********@hotmail.com (Edward) wrote in message news:<25**************************@posting.google. com>...
SQL Server 7.0

The following SQL:

SELECT TOP 100 PERCENT fldTSRID, fldDateEntered
FROM tblTSRs WITH (NOLOCK)
WHERE ((fldDateEntered >= CONVERT(DATETIME, '2003-11-21 00:00:00',
102))
AND
(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:59', 102)))

returns this record:

fldTSRID: 4
fldDateEntered: 24/11/2003

Hello? How is 24/11/2003 <= '2003-11-23 23:59:59'?

I tried decrementing the second predicate by seconds:

(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:30', 102)))

returns the record, but

(fldDateEntered <= CONVERT(DATETIME, '2003-11-23 23:59:29', 102)))

does NOT.

What is happening here?

Edward

============================

TABLE DEFINITION:

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblTSRNotes_tblTSRs]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblTSRNotes] DROP CONSTRAINT FK_tblTSRNotes_tblTSRs
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblTSRs]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [dbo].[tblTSRs]
GO

CREATE TABLE [dbo].[tblTSRs] (
[fldTSRID] [int] IDENTITY (1, 1) NOT NULL ,
[fldDealerID] [int] NOT NULL ,
[fldWorkshopGroupID] [int] NULL ,
[fldSubjectID] [int] NULL ,
[fldReasonID] [int] NULL ,
[fldFaultID] [int] NULL ,
[fldContactID] [int] NULL ,
[fldMileage] [int] NULL ,
[fldFirstFailure] [smalldatetime] NULL ,
[fldNumberOfFailures] [int] NULL ,
[fldTSRPriorityID] [int] NULL ,
[fldTSRStatusID] [int] NULL ,
[fldAttachedFilePath] [char] (255) NULL ,
[fldFileAttached] [smallint] NOT NULL ,
[fldFaultDescription] [ntext] NULL ,
[fldFaultRectification] [ntext] NULL ,
[fldEmergency] [int] NOT NULL ,
[fldDateEntered] [smalldatetime] NOT NULL ,
[fldEnteredBy] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


The smalldatetime data type is accurate to one minute - according to
BOL, a smalldatetime value is rounded down if the seconds are 29 or
less, or up or 30 or more seconds.

Although I don't have a SQL7 installation to test this, I seem to
remember that for comparisons, the column type took precedence over
the expression. So your datetime is converted to smalldatetime and
then compared. In SQL 2000, the reverse would happen, because data
type precedence is strictly defined, and datetime is higher than
smalldatetime.

Simon
Jul 20 '05 #4
David Portas (RE****************************@acm.org) writes:
Correction: Although my solution should be work, my explanation wasn't
accurate. It's true that the precision of DATETIME is limited to 3
milliseconds but that doesn't appear to be the root of your problem since
you haven't used milliseconds.

The precision of SMALLDATETIME is 1 minute but the value of '23:59:59' in
your query shouldn't get rounded up to the following midnight. Instead the
SMALLDATETIME value from your table should be implicitly cast to DATETIME
with no loss of precision. Your query is therefore sub-optimal since the
implicit conversion will be performed for every row. However, the query
should not produce the result you described.


But the rules for implicit conversion were changed from SQL7 to SQL2000,
and Edward runs SQL7.

I am not going to try to explain the conversion rules for SQL7, as this
was the version that I more or less skipped. However, testing I find
that:

create table #temp (a smalldatetime)
insert #temp values ('20031123')
insert #temp values ('20031124')
go
select * from #temp where a <=
CONVERT(DATETIME, '2003-11-23 23:59:59', 102)

return two rows on SQL7 on SQL 6.5 and one row on SQL2000.

David's suggestion to use:

SELECT fldtsrid, flddateentered
FROM tblTSRs
WHERE fldDateEntered >= '20031121'
AND fldDateEntered < '20031124'
is of course the bulletproof way of running the query.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
> But the rules for implicit conversion were changed from SQL7 to SQL2000,
and Edward runs SQL7.


Ah, the light dawns! Sorry, I missed that vital piece of info from Edward's
post.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #6
Hi All,

Why the "102"? I thought that was only needed to return the date in a
character format/style. Just curious.

CONVERT(DATETIME, '2003-11-23 23:59:59', 102)
Jul 20 '05 #7
louis nguyen (lo************@hotmail.com) writes:
Why the "102"? I thought that was only needed to return the date in a
character format/style. Just curious.

CONVERT(DATETIME, '2003-11-23 23:59:59', 102)


The 102 forces interpretation of the datetime literal according to that
format.

Using YYYYMMDD HH:MM:SS is easier - then you don't need to force any
format, and you can skip the convert.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8
te********@hotmail.com (Edward) wrote in message news:<25**************************@posting.google. com>...

<Snippage>

Thanks to you all for your great help, as usual.

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk
Jul 20 '05 #9

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

Similar topics

2
by: ai lian | last post by:
The code is as the following: { double limit= 0.02*33.00; double limit1= 0.06*11.00; double limit2= 0.03*22.00; double limit3= 0.01*66.00; limit=(floor(limit*100))/100;...
5
by: Coleen | last post by:
Hi all :-) I have a bit of code that chacks for the last day of the Month, and if it falls on a week-end, sets the due date to the Monday after... I'm trying to get the date to go to the...
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
2
by: rushaustin | last post by:
Hello, In SQL, if I do Select cast (37797.8159722222 as datetime)as DateFromDecimal i get 2003-06-27 19:34:59.997 as the return. in VB.NET if I do: Dim idate As Date idate =...
3
by: aling | last post by:
Execute following T-SQL within Queary Analyzer of SQL Server 2000: ======================================= DECLARE @dTest DATETIME SET @dTest='2001-1-1 1:1:1:991' SELECT @dTest SET...
10
by: alsmeirelles | last post by:
Hi all, I Have run this test: Private Sub test() Dim d As Double Dim f As Single Dim output As String d = 8888888888888.8887
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.