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

chana gravatar 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][1], it has a pretty detailed explanation about datetime math.


[1]: http://ask.sqlservercentral.com/questions/16420/php-with-mssql-strtotime-with-mssql-datetime-column
more ▼

answered Dec 07, 2010 at 07:53 PM

Oleg gravatar image

15.9k 2 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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: 858 times

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