Date Function


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, 2010 at 06:50 PM in Default

avatar image

49 8 9 9

(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)
     @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.


more ▼

answered Dec 07, 2010 at 07:53 PM

avatar image

20.5k 3 7 29

(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



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Dec 07, 2010 at 06:50 PM

Seen: 979 times

Last Updated: Dec 07, 2010 at 10:18 PM

Copyright 2018 Redgate Software. Privacy Policy