x
login about faq Site discussion (meta-askssc)

Date Function

Hi

I have 2 paramater as startdate and enddate After subtracting this ie @date = ( startdate- enddate) Now i want to conert it into @date=@date/60/60/24/365.

It say me datetime cannot be converted into int.

Kindly help me wiyth script

more ▼

asked Dec 07 '10 at 06:50 PM in Default

chana gravatar image

chana
49 7 8 8

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

It appears that you are looking for a difference in seconds between 2 dates. If this is so then you don't really need to divide the difference by /60/60/24/365. The difference between 2 datetime values is still a datetime, you cannot divide it by an integer. To get what you need, use datediff function instead:

declare @startdate datetime;
declare @enddate datetime;
declare @result int;

-- populate your parameters (start@date - an hour ago, @enddate - now)
select 
    @enddate = getDate(), 
    @startdate = dateadd(hour, -1, @enddate),
    @result = datediff(second, @startdate, @enddate);

-- now select your result, it will be equal to 3600 :)
select @result;

You can read the answer to this question, it has a pretty detailed explanation about datetime math.

Oleg

more ▼

answered Dec 07 '10 at 07:53 PM

Oleg gravatar image

Oleg
15.4k 1 4 24

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x84
x40

asked: Dec 07 '10 at 06:50 PM

Seen: 543 times

Last Updated: Dec 07 '10 at 10:18 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.