Solved

about select datediff(s,lastupdate,getdate()) from tcompanysupplier error!

Posted on 2006-07-17
4
590 Views
Last Modified: 2012-06-21
hi,experts
  in mytable tcompanysupplier the lastupdate column's value is 1900-01-01 00:00:00.000
when
 select datediff(s,lastupdate,getdate()) from tcompanysupplier
will show error,
because the result is bigint ,but the datediff function return int ,so will show error
how can i do it .i want write a self define function to do this,but i can not write ,can experts help me!

Best Regard!
0
Comment
Question by:hgbdelphi
4 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17120573
what datatype is lastupdate? is it a datetime?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17120582
Hi hgbdelphi,
Its because datediff function returns an  int value.
FROM BOL
DATEDIFF produces an error if the result is out of range for integer values. For milliseconds, the maximum number is 24 days, 20 hours, 31 minutes and 23.647 seconds. For seconds, the maximum number is 68 years.

Cheers!
0
 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 50 total points
ID: 17120586
Create an user-defined function using code like this:

DECLARE @SecondDiff BIGINT
DECLARE @LastUpdate DATETIME
DECLARE @CurrentDate DATETIME

SET @SecondDiff = (CONVERT(BIGINT, DATEDIFF(d, @LastUpdate, @CurrentDate)) * 24 * 60 * 60) + DATEDIFF(s, DATEADD(d, DATEDIFF(d, @LastUpdate, @CurrentDate), @LastUpdate), @CurrentDate)

You will need to pass the current date to the user-defined function because GETDATE() cannot be used inside an user-defined function

0
 

Author Comment

by:hgbdelphi
ID: 17126853
hi,Mr_Peerapol experts,
thanks for your help,it's work fine,

create function j_datediff(@LastUpdate DATETIME , @CurrentDate DATETIME )
returns bigint
as
begin
        declare @SecondDiff bigint
      SET @SecondDiff = (CONVERT(BIGINT, DATEDIFF(d, @LastUpdate, @CurrentDate)) * 24 * 60 * 60) + DATEDIFF(s, DATEADD(d, DATEDIFF(d, @LastUpdate, @CurrentDate), @LastUpdate), @CurrentDate)
        return @SecondDiff
End
go
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now