| Business Application Development with: SQL Server, C#, VB, VB.Net, ASP, ASP.Net, and XML |
|
|
News Links Schedule Site Map Contact |
|
SQL Server T-SQL User-Defined Function of the WeekConverting to a Julian DateVolume 2 Number 2 November 26, 2003Check out the UDF Frequently Asked Questions at:
http://www.novicksoftware.com/UdfOfWeek/UDF_FAQ.htm
Transact-SQL User-Defined Functions has been published! Take a look at it right now!
There are many formats used to store date and time information and this issue is about one of them, the Julian date. It's usually defined as the number of days since an origin day but I've seen people use the day of the year and call that the Julian date. The US Navy, an organization that has an driving concern to answer the question, "What time is it?" uses January 1, 4713 BCE as their origin date. You can view their calculator and read about how they calculate the date at this link: When you work with dates that are earlier than 1753 you'll have to handle changes in the calendar that have occurred since then. Unfortunately, the last set of changes didn't all happen at the same time as Pope Gregory had hoped so the date isn't really the same all over the European world. In any case when working with SQL Server it's a good idea to use the same origin date for a Julian as SQL Server uses for datetime and smalldatetime: January 1, 1900. Unfortunately, other Microsoft code, such as VBScript, Visual Basic and .Net use December 30, 1899 as the origin. Go figure? The purpose of this week's UDF is to convert a datetime value to the Julian date, which is an int. Here's the CREATE FUNCTION script:
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION dbo.udf_DT_ToJulian (
@Date datetime -- Date to convert to Julian
) RETURNS int -- Julian date. # days since 1900-01-01
WITH SCHEMABINDING -- This is a deterministic function.
/*
* Returns the Julian date. The number of days since 1990-01-01.
*
* Equivalent Template:DATEDIFF(dd,'1990-01-01',
Let's take a look at a few results from the function:
SELECT dbo.udf_DT_ToJulian ('2003-11-26') as [Today]
, dbo.udf_DT_ToJulian ('1900-01-01') as [Origin]
, dbo.udf_DT_ToJulian ('1812-01-01') as [Old Date]
, dbo.udf_DT_ToJulian ('2000-01-01') as [Y2K]
GO
(Results)
Today getdate Origin Old Date Y2K
----------- ----------- ----------- ----------- -----------
37949 37949 0 -32142 36524
One of the reasons for using the Julian date is that it drops all time values and only stores the date. You can see that in the column [getdate]. The lack of time makes Julian dates simple to work with in systems that don't care about the time. And of course, calculating the difference between dates is trivial, just subtract them. Can you guess what function is coming up next week? Of course, it's the converse function udf_DT_FromJulian. My book T-SQL UDFs is available on my web site bundled with the Bonus-100 library of UDFs. I've adjusted the shipping prices and can now ship to the entire world. For those of you in the US, have a great Thanksgiving. Do you have a great UDF that you'd like to share? Or maybe you have a T-SQL problem that you think could be solved by a UDF but you don't know how? Send them to: UDF@NovickSoftware.com and they might be published in this newsletter. I try and respond to every request that I get. Thanks, Andrew Novick
|
|
|
Copyright © 2003-2008 Novick Software, Inc. | Terms of Use | Privacy Policy | Nice Things People Say| |