Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-17
4
Medium Priority
?
606 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

730 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