Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 12th, 2005, 09:59 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
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!
Reply With Quote
  #2 (permalink)  
Old September 7th, 2005, 08:17 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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.



Reply With Quote
  #3 (permalink)  
Old September 8th, 2005, 02:56 AM
Authorized User
 
Join Date: Oct 2004
Location: Bristol, Nth. Somerset, United Kingdom.
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.
Reply With Quote
  #4 (permalink)  
Old September 8th, 2005, 01:55 PM
Registered User
 
Join Date: Sep 2005
Location: Pittsburgh, PA, USA.
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



Reply With Quote
  #5 (permalink)  
Old September 8th, 2005, 02:09 PM
Registered User
 
Join Date: Sep 2005
Location: Pittsburgh, PA, USA.
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



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:34 PM.


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