Solved

Problem with decimal portion of seconds in date

Posted on 2008-10-27
6
296 Views
Last Modified: 2010-08-05
I am working on a VB6 - ADO program that ties in with some stored proceedures (developed by a third party).  The problem that I am running into is that when the stored proceedure populates a datetime field it records the seconds to 3 decimal points, for example 09/01/2008 09:29:25.233.  When I use visual basic to retrieve this field and then try to run a query using the result it seems like it is cutting off the decimal portion of the seconds and giving me an empty record set.

example

SQL Server table:

Field1                     ModifyDateTime
Test                       09/01/08 09:29:25.233

Using VB6 and an ADO Query
sqltext = "Select * from myTable where field1 = 'Test'"
myRS.open sqltext, myConnection, adOpenDynamic, adLockOptimistic

#At this point one record is returned, so I note the modify datetime
tmpdte = myRS!ModifyDateTime

#now we run a query using the returned value
sqltext = "Select * from myTable where ModifyDateTime = '" & tmpdte & "'"
myRS2.open sqltext, myConnection, adOpenDynamic, adLockOptimistic

#this returns no records, however if I change the value in the modifydatetime field to 09/01/08 09:29:25 then it works fine.

Does anyone have any ideas how to get around this.

0
Comment
Question by:dsgvwf
  • 4
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Try this as the decimal portion is number of milliseconds.  What is likely happening is your date time is only to seconds precision and so not able to match directly to the data in table at 9:25:00.000 doesn't equal 9:25:00.233.
sqltext = "Select * from myTable where DateDiff(s, ModifyDateTime, '" & tmpdte & "') = 0"

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
This will use DateDiff (http://msdn.microsoft.com/en-us/library/ms189794.aspx) to check the difference in the times in seconds.  This should yield 0 between 9:25:00.000 and 9:25:00.233.

Hope that helps.
0
 

Author Comment

by:dsgvwf
Comment Utility
My VB6 routine will be making use of several stored proceedures to update records in the database that will depend on matching the datetime fields exactly.  Is there anyway to adjust the precision in VB6 to match the milliseconds?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
Comment Utility
It has been a while for straight VB6 and not .NET, but in VB6 I think the DateAdd function only takes iintervals down to s for second, but you can see if there is a millisecond portion that I am missing.  You could just add appropriate milliseconds to your code.  I don't know of a way though as everything for VB6 I see only goes to the second.  VB.NET has functionality if you can use that.

Otherwise, you will have do it on the database side.  In which case you can create a User Defined Function that returns a bit indicating if a datetime matches down to seconds then you can use the udf in code everywhere you need it.
0
 

Author Comment

by:dsgvwf
Comment Utility
I think I have found a work around.

When get the returned modifydatetime I convert it to a double using CDbl then use this to calcualte the millisecond portion, and build a string for the date instead of using a date type.  When I create the parameter for my stored proceedure, I declare it as a adVarChar instead of adDBTimeStamp and it seems to run ok.  I assume the variable is being forced into a date type on the server side.

It is not the most elegant solution but it seems to work.

Thanks for your help, even though you didn't provide the exact answer I was looking for, you gave me a direction I had not thought to go in.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
It sure does.  Meant to post that, but I didn't so :(.

What I had thought was you could add a field onto your original query to get the ModifyDateTime that gets the millisecond part.  You can use DatePart(ms, ModifyDateTime) then you can just concatenate it to the end of your date.  If it makes it easier.  May be same amount of effort.

http://msdn.microsoft.com/en-us/library/ms174420.aspx
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now