?
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,514 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

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!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

771 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