Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function to convert date from text format YYYYDDD where DDD = number of days in the year

Posted on 2007-11-20
14
Medium Priority
?
355 Views
Last Modified: 2008-03-03
eg. select dbo.CnvDateYYYYDDD('2007305')  will return 31-Oct-2007

Thanks



0
Comment
Question by:xenium
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
14 Comments
 

Author Comment

by:xenium
ID: 20322507
i should say, where DDD = day of year !
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20322514
replace(convert(varchar(10), convert(datetime, '2007305', 112), 106), ' ', '-')
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20322562
sorry, posted to fast...

create function dbo.CnvDateYYYYDDD(@v varchar(7))
returns varchar(11)
as
begin
 declare @d datetime
 declare @res varchar(11)
 set @d = dateadd(day, cast(right(@v, 3) as int) -1, convert(datetime, left(@v,4) + '01-01', 120))
 set @v = replace(convert(varchar(11), @v, 106), ' ', '-')
 return @v
end

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:xenium
ID: 20322615
select dbo.CnvDateYYYYDDD('2007305')

Server: Msg 241, Level 16, State 1, Procedure CnvDateYYYYDDD, Line 7
Syntax error converting datetime from character string.


0
 

Author Comment

by:xenium
ID: 20322730
this seems to work (several edits!)..

create function dbo.CnvDateYYYYDDD(@v varchar(7))
returns varchar(11)
as
begin
 declare @d datetime
 declare @res varchar(11)
 set @d = dateadd(day, cast(right(@v, 3) as int)-1, convert(datetime, left(@v,4) + '-01-01', 120))
 set @res = replace(convert(varchar(11), @d, 106), ' ', '-')
 return @res
end

0
 

Author Comment

by:xenium
ID: 20322742
Correction also, select dbo.CnvDateYYYYDDD('2007305') correctly returns 1st-Nov-2007
0
 

Author Comment

by:xenium
ID: 20322780
Thanks for helping get the solution. Copy-paste the above corrected function for the points (just makes PAQ simpler)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20322797
I see I had
 set @res = replace(convert(varchar(11), @v, 106), ' ', '-')

instead of
 set @res = replace(convert(varchar(11), @d, 106), ' ', '-')

sorry for that typo, I wrote the code directly here without checking..
create function dbo.CnvDateYYYYDDD(@v varchar(7))
returns varchar(11)
as
begin
 declare @d datetime
 declare @res varchar(11)
 set @d = dateadd(day, cast(right(@v, 3) as int)-1, convert(datetime, left(@v,4) + '-01-01', 120))
 set @res = replace(convert(varchar(11), @d, 106), ' ', '-')
 return @res
end

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 20322837
Functions should be written *for efficiency* -- no extraneous variables, code, etc. -- rather than to show step-by-step logic.  For example:
CREATE FUNCTION dbo.CnvDateYYYYDDD(
    @yyyyddd varchar(7)
)
RETURNS varchar(11)
AS
BEGIN
RETURN REPLACE(CONVERT(VARCHAR(11), 
    DATEADD(DAY, CAST(RIGHT(@yyyyddd, 3) AS INT) - 1, LEFT(@yyyyddd, 4) + '0101')
    , 106), ' ', '-')
END

Open in new window

0
 

Author Comment

by:xenium
ID: 20323005
Scott, thanks for the suggestion, i agree efficiency is important. Though in many cases human efficiency is more valuable than machine efficiency, so clearer coding may be preferred.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 20323254
>>  Though in many cases human efficiency is more valuable than machine efficiency <<

I wouldn't recommend that for functions -- you could end up noticeably slowing down your system.  When you start applying a function to every row of a table, efficiency can make a *big* difference, especially when the table goes from 100 rows to 100,000 or 1M rows for some reason.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20323298
I do agree with ScottPletcher.
you should consider writing the function with the most effective code, and comment with the more human-readable version...
0
 

Author Comment

by:xenium
ID: 20324137
Ok thanks, fair point. Follow-up 1 coming up, as i need the output as Datetime..
0
 

Author Comment

by:xenium
ID: 20324150
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question