473,753 Members | 8,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird date rounding

SQL Server 7.0

The following SQL:

SELECT TOP 100 PERCENT fldTSRID, fldDateEntered
FROM tblTSRs WITH (NOLOCK)
WHERE ((fldDateEntere d >= CONVERT(DATETIM E, '2003-11-21 00:00:00',
102))
AND
(fldDateEntered <= CONVERT(DATETIM E, '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(DATETIM E, '2003-11-23 23:59:30', 102)))

returns the record, but

(fldDateEntered <= CONVERT(DATETIM E, '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 ,
[fldWorkshopGrou pID] [int] NULL ,
[fldSubjectID] [int] NULL ,
[fldReasonID] [int] NULL ,
[fldFaultID] [int] NULL ,
[fldContactID] [int] NULL ,
[fldMileage] [int] NULL ,
[fldFirstFailure] [smalldatetime] NULL ,
[fldNumberOfFail ures] [int] NULL ,
[fldTSRPriorityI D] [int] NULL ,
[fldTSRStatusID] [int] NULL ,
[fldAttachedFile Path] [char] (255) NULL ,
[fldFileAttached] [smallint] NOT NULL ,
[fldFaultDescrip tion] [ntext] NULL ,
[fldFaultRectifi cation] [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 5497
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********@hotm ail.com (Edward) wrote in message news:<25******* *************** ****@posting.go ogle.com>...
SQL Server 7.0

The following SQL:

SELECT TOP 100 PERCENT fldTSRID, fldDateEntered
FROM tblTSRs WITH (NOLOCK)
WHERE ((fldDateEntere d >= CONVERT(DATETIM E, '2003-11-21 00:00:00',
102))
AND
(fldDateEntered <= CONVERT(DATETIM E, '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(DATETIM E, '2003-11-23 23:59:30', 102)))

returns the record, but

(fldDateEntered <= CONVERT(DATETIM E, '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 ,
[fldWorkshopGrou pID] [int] NULL ,
[fldSubjectID] [int] NULL ,
[fldReasonID] [int] NULL ,
[fldFaultID] [int] NULL ,
[fldContactID] [int] NULL ,
[fldMileage] [int] NULL ,
[fldFirstFailure] [smalldatetime] NULL ,
[fldNumberOfFail ures] [int] NULL ,
[fldTSRPriorityI D] [int] NULL ,
[fldTSRStatusID] [int] NULL ,
[fldAttachedFile Path] [char] (255) NULL ,
[fldFileAttached] [smallint] NOT NULL ,
[fldFaultDescrip tion] [ntext] NULL ,
[fldFaultRectifi cation] [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(DATETIM E, '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(DATETIM E, '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(DATETIM E, '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********@hotm ail.com (Edward) wrote in message news:<25******* *************** ****@posting.go ogle.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
1464
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; limit1=(floor(limit1*100))/100;
5
1565
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 Tuesday afterward, if the last day of the month falls on the week-end and the Monday afterwards is a holiday... to test I set the date to 11/30/2006, to make the due date fall on 12/31/2006 which is a Sunday and makes 1/1/2007 a holiday. I've put in the...
7
2770
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, here is my table structure, Name: Table1
11
6655
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 seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
2
7751
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 = Date.FromOADate(37797.8159722222) I get 6/25/2003 7:35:00 PM as the return.
3
5487
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 @dTest='2001-1-1 1:1:1:997' SELECT @dTest
10
2163
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
13297
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) It then updates the value with numberOfPrecisions after the decimal
20
5007
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
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9421
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9333
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.