Solved

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

Posted on 2006-07-17
4
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

635 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