x

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

avatar image

chana
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)
 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, 2010 at 07:53 PM

avatar image

Oleg
16.8k 3 7 28

(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

SQL Server Central

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

Topics:

x80
x30

asked: Dec 07, 2010 at 06:50 PM

Seen: 967 times

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

Copyright 2016 Redgate Software. Privacy Policy