Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2007, 02:20 AM
Authorized User
 
Join Date: Dec 2006
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default DDD-YYYY

Hi,

I have Date in format DDD-YYYY (varchar field in the database)
The valid dates are 010-2005, 100-1998, ...
DDD can be between 1 and 365 and YYYY can be between 1900 and 2010.

Is there a way to convert this format to DDMMYYYY or MMDDYYYY or any other valid formats which SQL SERVER understand, so that i can make use of the function ISDATE()

Thanks in advance



Chandru
__________________
Thanks,
Chandra
 
Old January 23rd, 2007, 03:15 PM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following:

DECLARE @DDD_YYYY VARCHAR(8)
SET @DDD_YYYY = '010-2007'
SELECT CAST(RIGHT(@DDD_YYYY, 4) + '/01/01' AS DATETIME) + CAST(LEFT(@DDD_YYYY, 3) AS INT)

For other SQL Server Date Formats:
http://www.sql-server-helper.com/tips/date-formats.aspx

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old January 24th, 2007, 12:34 PM
Authorized User
 
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can parse the string into days and year. Set the date to 1/1/YYYY for the given year. Then use DATEADD to add the total days. Then you can format the date however you like with CONVERT.

Example:
DECLARE @str varchar(8)
SET @str = '089-2001'
SELECT DATEADD(d, CONVERT(smallint, SUBSTRING(@str, 1, 3)), CONVERT(datetime, '1/1/' + SUBSTRING(@str, 5, 4)))


Adam Gossage
Lake Wylie, SC, USA
 
Old January 25th, 2007, 10:58 AM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you look at the code that I gave and compare it to the code agossage provided, it provides the same result. My script returns the string in datetime function. I just didn't use the DATEADD function because using the + basically does the same thing, add a number of days to the date. Try my code and let me know if it doesn't give the same result.

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old January 25th, 2007, 11:19 AM
Authorized User
 
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're right, I just gave another method. I think the calculation is wrong in both cases.

The days portion of the string is the day of the year. If we start at the 1st day, our calculation is one day off. Since 1/0/YYYY is not a valid date, we would have to subtract 1.

Please correct me if I am wrong.

Correct Example:
DECLARE @str varchar(8)
SET @str = '010-2001'
SELECT DATEADD(d, CONVERT(smallint, SUBSTRING(@str, 1, 3)) - 1, CONVERT(datetime, '1/1/' + SUBSTRING(@str, 5, 4)))


Adam Gossage
Lake Wylie, SC, USA





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert dd/MM/yyyy to MM/dd/yyyy Rg2005 VB.NET 2002/2003 Basics 2 November 7th, 2008 06:42 PM
mm/dd/yyyy to dd/mm/yyyy Ashleek007 Beginning PHP 2 September 27th, 2006 06:35 AM
convert "MM/dd/yyyy" to "dd/MM/yyyy" xin56 General .NET 4 March 28th, 2005 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.