Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Policy Based Management in SQL Server 2008
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...
Using Column Sets with Sparse Columns

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> developer >> How to Fix the Problem When ADO ...

How to Fix the Problem When ADO Truncates Milliseconds from SQL Server Datetime Columns

By : Brian Lockwood
Jul 20, 2002

Page 2 / 2

SOLUTION 3 - Use a Custom VB Function

Apparently, the only way to do this is to do it ourselves with a custom VB function that infers the milliseconds using a moderately complex algorithm (see below).

But the results are right on, as you can see from this example:

? FormatDateTimeWithMS(rs(0))

20020809 23:15:27:490

Heres the code for this custom VB function. I hope you find it useful.

 

Public Function FormatDateTimeWithMS(ByVal dDate As Date, Optional strFormat)

Dim lMilliseconds As Long
Dim bSecondRoundedToUpper As Boolean
lMilliseconds = GetMilliseconds(dDate, bSecondRoundedToUpper)

' if the second gets rounded to upper substract one second
If bSecondRoundedToUpper Then
dDate = DateAdd("s", -1, dDate)
End If

' check of the strFormat is missing
If Not IsMissing(strFormat) Then
FormatDateTimeWithMS = Format(dDate, strFormat)
Else
FormatDateTimeWithMS = Format(dDate, "yyyymmdd Hh:Nn:Ss") ' You can add your own format
End If

FormatDateTimeWithMS = FormatDateTimeWithMS & ":" & Format(lMilliseconds, "000000")

End Function


Public Function GetMilliseconds(dOriginalDate As Date, ByRef bSecondRoundedToUpper As Boolean) As Long

' get the string (without milliseconds)
Dim strDateTime As String
strDateTime = CStr(dOriginalDate)

' return it to the datetime (this one doesn't have milliseconds)
Dim dRoundedDateTime As Date
dRoundedDateTime = CDate(strDateTime)

' check if we're below the zero
If dOriginalDate > 0 Then
' check if the rounded date is after the original
If dRoundedDateTime > dOriginalDate Then
' substract one second
dRoundedDateTime = DateAdd("s", -1, dRoundedDateTime)

' return the flag set to on
bSecondRoundedToUpper = True
Else
' return the flag set to off
bSecondRoundedToUpper = False
End If
Else
' check if the rounded date is before the original
If dRoundedDateTime < dOriginalDate Then
' add one second (by deducting it! - bug in VB?)
dRoundedDateTime = DateAdd("s", -1, dRoundedDateTime)

' return the flag set to on
bSecondRoundedToUpper = True
Else
' return the flag set to off
bSecondRoundedToUpper = False
End If

End If

' milliseconds are the number of ms in one day by the difference of the rounded and the orignal date
GetMilliseconds = 86400000 * Abs(dOriginalDate - dRoundedDateTime)
End Function

' TEST SUB
Public Sub TestMilliseconds

Dim cn As New ADODB.Connection
cn.ConnectionString = "driver={SQL Server};server=(local);uid=sa;pwd=q;database=pubs"
cn.Open

Dim cm As New ADODB.Command
Set cm.ActiveConnection = cn
cm.CommandText = "select getdate(), convert(varchar,getdate(),21)"
cm.CommandType = adCmdText

Dim rs As ADODB.Recordset
Set rs = cm.Execute

Dim strVBFormat As String
strVBFormat = FormatDateTimeWithMS(rs(0).Value)

MsgBox "SERVER FORMAT: " & rs(1).Value & vbCrLf & "VB FORMAT: " & strVBFormat

End Sub

 

Published with the express written permission of the author. Copyright 2002


<< Prev Page         








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved