Here's a function that will return the military julian equivalent of any date passed to it. The default is to return the current day's.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Description: Function to return Military Julian in the format of YYDDD
-- RETURNS: varchar data type
-- INPUTS: date or default
-- Example Using Today's Date: select dbo.fnGetMilitaryJulian(default)
-- Example Date: select dbo.fnGetMilitaryJulian('01/08/2008')
-- Example Leap Year: select dbo.fnGetMilitaryJulian('12/31/2012')
-- =============================================
ALTER FUNCTION [dbo].[fnGetMilitaryJulian]
(
-- Add the parameters for the function here
@theDate datetime = null
)
RETURNS varchar(5)
AS
BEGIN
-- Declare the ordinal part of the variable
DECLARE @miljul varchar(3)
-- Description: Function to return Military Julian in the format of YYDDD
-- RETURNS: varchar data type
-- INPUTS: date or default
-- Example Using Today's Date: select dbo.fnGetMilitaryJulian(default)
-- Example Date: select dbo.fnGetMilitaryJulian('01/08/2008')
-- Example Leap Year: select dbo.fnGetMilitaryJulian('12/31/2012')
-- =============================================
ALTER FUNCTION [dbo].[fnGetMilitaryJulian]
(
-- Add the parameters for the function here
@theDate datetime = null
)
RETURNS varchar(5)
AS
BEGIN
-- Declare the ordinal part of the variable
DECLARE @miljul varchar(3)
--treat an empty string or spaces as a null
if rtrim(ltrim(@theDate)) = ''
set @theDate = NULL
DECLARE @d varchar(2);
DECLARE @m varchar(2);
DECLARE @y int;
DECLARE @m varchar(2);
DECLARE @y int;
DECLARE @firstofyear datetime;
DECLARE @yearpart varchar(2);
DECLARE @tempjul varchar(5);
DECLARE @tempjul varchar(5);
set @m = 01;
set @d = 01;
set @d = 01;
-- set year part on the date passed in
if @theDate is not null
BEGIN
set @y = year(@theDate);
set @yearpart = RIGHT(convert(varchar(4),year(@theDate)),2);
END
else
BEGIN
-- set year part for today
set @y = year(getdate());
set @yearpart = RIGHT(convert(varchar(4),year(getdate())),2);
END
-- finally determine the first day of the year requested
select @firstofyear = convert(datetime,@m +'-'+ @d +'-'+ convert(varchar,@y))
if @theDate is not null
BEGIN
set @y = year(@theDate);
set @yearpart = RIGHT(convert(varchar(4),year(@theDate)),2);
END
else
BEGIN
-- set year part for today
set @y = year(getdate());
set @yearpart = RIGHT(convert(varchar(4),year(getdate())),2);
END
-- finally determine the first day of the year requested
select @firstofyear = convert(datetime,@m +'-'+ @d +'-'+ convert(varchar,@y))
-- get the ordinal day from the constructed date
if @theDate is not null
BEGIN
SELECT @miljul = convert(varchar(3),datediff(day,@firstofyear, @theDate) + 1)
END
else
BEGIN
-- set miljul for today
SELECT @miljul = convert(varchar(3),datediff(day,@firstofyear, getdate()) + 1)
END
if @theDate is not null
BEGIN
SELECT @miljul = convert(varchar(3),datediff(day,@firstofyear, @theDate) + 1)
END
else
BEGIN
-- set miljul for today
SELECT @miljul = convert(varchar(3),datediff(day,@firstofyear, getdate()) + 1)
END
-- insure the ordinal day is 3 digits long
set @miljul = right(replicate('0',3) + @miljul,3)
set @miljul = right(replicate('0',3) + @miljul,3)
RETURN @yearpart + @miljul
END
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment