Click here to Skip to main content
5,788,961 members and growing! (20,788 online)
Email Password   helpLost your password?
Database » Database » Databases     Intermediate

Convert DateTime To .NET Ticks Using T-SQL

By Eron Wright

Convert a datetime value to .NET-compatible Ticks using a T-SQL User-Defined Function. Useful for interoperation between .NET and unmanaged environments such as ASP and VB6
SQL, Windows, .NET 1.0, .NET 1.1, .NETSQL Server, SQL 2000, DBA, Dev

Posted: 28 Dec 2003
Updated: 28 Dec 2003
Views: 73,284
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.13 Rating: 3.13 out of 5
4 votes, 40.0%
1
0 votes, 0.0%
2
1 vote, 10.0%
3
2 votes, 20.0%
4
3 votes, 30.0%
5

Introduction

The .NET Framework provides a simple long representation for a datetime, known as Ticks. A given ticks value is the number of 100-nanosecond intervals that have elapsed since 12:00 A.M., January 1, 0001. Unmanaged components might have to interact with .NET components using ticks. This article presents a simple T-SQL User-Defined Function for converting a datetime value to ticks.

Background

Unmanaged languages often expose datetime values as OLE Automation dates (OADate). An OLE Automation date is implemented as a floating-point number whose value is the number of days from midnight, 30 December 1899. In 32-bit systems, a Ticks representation is higher-fidelity than an OADate representation. Consequently, a given datetime value should be converted to Ticks from within SQL Server, not unmanaged application code.

Using the code

Create the User-Defined Functions supplied with this article on a SQL Server database. Use the dbo.DateTimeToTicks function in queries, views, and stored procedures as needed.

DECLARE @TestDate datetime
SET @TestDate = GetDate()
select @TestDate, dbo.DateTimeToTicks(@TestDate)

Points Of Interest

For information on the .NET Framework DateTime structure, see here. Documentation on Ticks and OADates is here and here.

History

  • Release 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Eron Wright


I am a Senior Software Architect for Point2 Technologies, a software development company in Saskatoon, Saskatchewan, Canada.

I love .NET, SQLXML, XSLT, Web Services, Java and much more.
Occupation: Web Developer
Location: Canada Canada

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralTicks to DateTimememberRicardo Casquete19:33 19 Nov '08  
GeneralOther idea for Ticksmembercool_greg0:14 21 Apr '08  
GeneralTo what end?membercode slave5:38 18 Dec '05  
Generalconvert ticks to DateTimemembernmrs24:04 12 Jan '04  
GeneralRe: convert ticks to DateTimememberGuido_d3:43 14 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Dec 2003
Editor: Nishant Sivakumar
Copyright 2003 by Eron Wright
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project