Technical Article

Julian and Gregorian Conversion Functions

,

After seeing a thread in the forums about converting a Julian date to Gregorian, I decided to write these functions. There are two functions in the script, getJulian and getGregorian. getJulian accepts a datetime parameter and returns the Julian date as an integer. getGregorian accepts an integer and returns the Gregorian date as datetime. A Code sample is included in the script.
Steve

/*
Usage examples -
DECLARE
@Date datetime,
@Julian int

SET @Date = '2004-08-03'

SELECT @Julian = dbo.getJulian(@Date)
SELECT @Julian

SELECT dbo.getGregorian(@Julian)
*/

CREATE FUNCTION getJulian (@Date datetime)
RETURNS INT
AS
BEGIN
DECLARE @Julian int

SELECT @Julian =   CAST(CAST(DATEPART(YEAR, @Date)AS char(4)) + '000' AS int)
+ DATEPART(dayofyear, @Date)
RETURN(@Julian)
END
GO


CREATE FUNCTION getGregorian (@Julian int)
RETURNS DATETIME
AS
BEGIN
DECLARE @Date datetime

SELECT @Date = DATEADD(DAY, substring(cast(@Julian as char(7)),5,3) - 1, '01/01/' + substring(cast(@Julian as char(7)),1,4))

RETURN(@Date)
END
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating