Solved

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

Posted on 2006-07-17
4
594 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email about the whoisactive result 7 29
Update data using formula 22 23
Help Parsing a String with SQL Syntax 23 33
conditional join based on column 4 12
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

863 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

20 Experts available now in Live!

Get 1:1 Help Now