In the company I used to work we are using 2 different calendars - Gregorian and Julian. We've been using Gregorian dates for productions such as pack dates in can codes. I will not discuss further about pack dates and can codes since its beyond the scope of this tutorials. Lets begin by creating some simple TSQL.
Below TSQL Scripts will covert our @date into julian and @julian into Gregorian date. More explanations here.
DECLARE @date datetime,
@julian char(5)
SET @date = '3/20/2012'
SET @julian = '12080'
--SELECT DATEPART(yy, @date), DATEPART(dy, @date)
--SELECT RIGHT(CAST(DATEPART(yy, @date) AS char(4)),2)
--SELECT RIGHT('000' + CAST(DATEPART(dy, @date) AS varchar(3)),3)
SELECT RIGHT(CAST(YEAR(@date) AS CHAR(4)),2) + RIGHT('000' + CAST(DATEPART(dy, @date) AS varchar(3)),3)
--SELECT LEFT(@julian,2), RIGHT(@julian,3)
--SELECT CONVERT(datetime, LEFT(@julian,2) + '0101', 112)
--SELECT DATEADD(day, CAST(RIGHT(@julian,3) AS int) - 1, CONVERT(datetime, LEFT(@julian,2) + '0101', 112))
SELECT DATEADD(day, CAST(RIGHT(@julian,3) AS int) - 1, CONVERT(datetime, LEFT(@julian,2) + '0101', 112))
Below TSQL Scripts will covert our @date into julian and @julian into Gregorian date. More explanations here.
DECLARE @date datetime,
@julian char(5)
SET @date = '3/20/2012'
SET @julian = '12080'
--SELECT DATEPART(yy, @date), DATEPART(dy, @date)
--SELECT RIGHT(CAST(DATEPART(yy, @date) AS char(4)),2)
--SELECT RIGHT('000' + CAST(DATEPART(dy, @date) AS varchar(3)),3)
SELECT RIGHT(CAST(YEAR(@date) AS CHAR(4)),2) + RIGHT('000' + CAST(DATEPART(dy, @date) AS varchar(3)),3)
--SELECT LEFT(@julian,2), RIGHT(@julian,3)
--SELECT CONVERT(datetime, LEFT(@julian,2) + '0101', 112)
--SELECT DATEADD(day, CAST(RIGHT(@julian,3) AS int) - 1, CONVERT(datetime, LEFT(@julian,2) + '0101', 112))
SELECT DATEADD(day, CAST(RIGHT(@julian,3) AS int) - 1, CONVERT(datetime, LEFT(@julian,2) + '0101', 112))
No comments:
Post a Comment