Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Part II: Function to convert date from text format YYYYDDD where DDD = day of year

Posted on 2007-11-20
8
Medium Priority
?
1,542 Views
Last Modified: 2011-09-20
eg. select dbo.CnvDateYYYYDDD('2007032')  will return 1-Feb-2007 as a date

Can you pls update the following so that DateTime format is returned? http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_22973663.html

Thanks!
0
Comment
Question by:xenium
  • 6
  • 2
8 Comments
 

Author Comment

by:xenium
ID: 20324234
i think its just this:

alter FUNCTION dbo.CnvDateYYYYDDD(
    @yyyyddd varchar(7)
)
RETURNS datetime
AS
BEGIN
RETURN     DATEADD(DAY, CAST(RIGHT(@yyyyddd, 3) AS INT) - 1, LEFT(@yyyyddd, 4) + '0101')
   
END
0
 

Author Comment

by:xenium
ID: 20324462
Since efficiency was a theme, might there be a faster way to code this if the input were an integer ?
eg select dbo.CnvDateYYYYDDD(2007032)

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20326365
your code is fine, choosing integer as data type will only add more implicit cases for the left and right functions...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:xenium
ID: 20326534
I'm thinking with integer we coudl avoid string operations completely, eg year = yyyyddd/1000, day = yyyyddd - 1000*year

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20326546
a good point, actually.

consider the modulo operator
day = yyyyddd % 1000  

0
 

Author Comment

by:xenium
ID: 20326600
Ok thanks. Here then is the integer version of the function:


alter FUNCTION dbo.CnvDateYYYYDDD(
    @yyyyddd int
)
RETURNS datetime
AS
BEGIN
RETURN     DATEADD(DAY, @yyyyddd % 1000 - 1, DATEADD(YEAR,@yyyyddd/1000 -1900,0))
END

0
 

Author Comment

by:xenium
ID: 20326610
I'm leaving this open a bit, coz i may just do a benchmark test vs the string functions, and note the results here for the record.
0
 

Author Comment

by:xenium
ID: 22330100
no time for benchmark, sorry.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

886 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