반응형

-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE function [dbo].[DATETIME_TO_JULIAN](@date as datetime) returns int 
as 
begin 
-- DataTime 값을 줄리안데이트 형식으로 변환 
declare @return as int
, @tempdate as char(8)
, @basedate as datetime
, @tempyyyy as int
, @interval as int 

set @tempdate = convert(char(8), @date, 112) 
set @tempyyyy = year(@date) -- cast(left(@tempdate, 4) as int) 
set @basedate = convert(datetime, left(@tempdate, 4) + '0101', 112) 
set @interval = datediff(d, @basedate, @date) + 1 
set @return = (@tempyyyy - 2000) * 1000 + @interval 
return @return 

end

GO

실행

SELECT [dbo].[DATETIME_TO_JULIAN]('20200630')

값 : 20182

반응형
반응형

-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[JULIAN_TO_DATETIME](@date as int) RETURNS DATETIME 
AS 
BEGIN 
-- 줄리안데이트를 DateTime으로 변환 
declare @return as datetime, -- Return Value 
@tempyyyy as int, -- Year 
@interval as int 

-- Year, Month, Day 계산 
set @tempyyyy = 2000 + convert(int, @date / 1000) 
set @interval = convert(int, right(convert(varchar(6), @date), 3)) - 1 
set @return = dateadd(d,@interval,convert(datetime,convert(char(4),@tempyyyy)+'0101',112)) 

return @return 
END

GO

 

실행

SELECT JULIAN_TO_DATETIME('20135')

값 : 2020-05-14 00:00:00.000

반응형

+ Recent posts