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 July 12th, 2005, 09:59 AM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert to days

Hi group!

If I have this 040201 which is 4 yrs 2 months and 1 day, I would like to know how many day from the number above. Is there any function in SQL which I can use to convert to the days? Thanks for all helps!
Ex:
040101 = 1492 days
000101 = 31 days
000001 = 1 days

Thanks!
 
Old September 7th, 2005, 08:17 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't think the question is logical. In the first example the number of days would depend if there were a leap year or not. 040101 (four years, 2 months and a day. What if it were one year, no months and no days. The number of days would depend if there were a leap year in the particular calander year.



 
Old September 8th, 2005, 02:56 AM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the second example the answer could be different depending on the number of days in a month.
 
Old September 8th, 2005, 01:55 PM
Registered User
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


declare @YourFormat char(6)
declare @RealDate smalldatetime

set @YourFormat = '040201'

set @RealDate = dateadd(dd, cast(substring(@YourFormat, 5, 2) as int), dateadd(mm, cast(substring(@YourFormat, 3, 2) as int), dateadd(yy, cast(substring(@YourFormat, 1, 2) as int), CURRENT_TIMESTAMP)))

print cast(@RealDate as varchar(21)) + ' is today plus ' + @YourFormat



 
Old September 8th, 2005, 02:09 PM
Registered User
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

-- If you just want to assume 365 days in a year and 30 days in a month
-- then:

declare @NumDays int
declare @YourFormat char(6)

set @YourFormat = '040201'

set @NumDays = cast(substring(@YourFormat, 1, 2) as int) * 365
                + cast(substring(@YourFormat, 3, 2) as int) * 30
                + cast(substring(@YourFormat, 5, 2) as int)

print @NumDays








Similar Threads
Thread Thread Starter Forum Replies Last Post
days-from-duration Dinghus XSLT 1 November 21st, 2007 04:05 AM
how to convert date to days teamml SQL Server 2000 3 May 8th, 2007 07:05 AM
Last 7 Days Record prasanta2expert Access 1 October 23rd, 2006 05:38 AM
Days in a Month jmss66 Classic ASP Basics 3 April 11th, 2005 05:57 PM
No of days anandham SQL Server 2000 2 March 21st, 2005 01:20 AM





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