Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need a help with this function

Posted on 2006-04-19
3
Medium Priority
?
233 Views
Last Modified: 2012-05-05
Hello Guys.
I made a function that must return me the difference between two dates and return me it formated. But sometimes when I have only minutes in their difference it is not working, only returning me 0. Could you take a look where should I improve it? Thanks

CREATE FUNCTION DIFHOURS (@Dt1 Datetime, @Dt2 DateTime)  
RETURNS char(10) AS  
BEGIN
  DECLARE @HOUR CHAR(10)
 
  IF DATEDIFF(SECOND, @Dt1, @Dt2) / 3600 = 0
     BEGIN
       SET @HOUR = 0
     END
  ELSE
     BEGIN
       SET @HOUR =  CAST(DATEDIFF(SECOND, @Dt1, @Dt2) / 3600 AS VARCHAR(2)) + ':' +
                    RIGHT('0' + CAST(DATEDIFF(SECOND, @Dt1, @Dt2) %3600 / 60 AS VARCHAR(2)), 2) + ':' +
                    RIGHT('0' + CAST(DATEDIFF(SECOND, @Dt1, @Dt2) % 60 AS VARCHAR(2)), 2)
     END
 
  RETURN @HOUR
END
0
Comment
Question by:hidrau
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 16487969

CREATE FUNCTION DIFHOURS (@Dt1 Datetime, @Dt2 DateTime)  
RETURNS char(10) AS  
BEGIN
  DECLARE @HOUR CHAR(10)
 
  IF DATEDIFF(SECOND, @Dt1, @Dt2) / 3600 = 0
     BEGIN
       SET @HOUR = '0:00:00'
     END
  ELSE
     BEGIN
       SET @HOUR =  CAST(DATEDIFF(SECOND, @Dt1, @Dt2) / 3600 AS VARCHAR(2)) + ':' +
                    RIGHT('0' + CAST(DATEDIFF(SECOND, @Dt1, @Dt2) %3600 / 60 AS VARCHAR(2)), 2) + ':' +
                    RIGHT('0' + CAST(DATEDIFF(SECOND, @Dt1, @Dt2) % 60 AS VARCHAR(2)), 2)
     END
 
  RETURN @HOUR
END
0
 
LVL 1

Author Comment

by:hidrau
ID: 16488111
angelIII I think you didn't understand, when the difference is only in minutes it is returning me none.

If I have this two date at this format: DD/MM/YYYY

1 date: 01/01/2006 15:35:00  
2 date: 01/01/2006 15:36:00

the difference between is 2 minutes and the function is returning me 0
0
 
LVL 1

Author Comment

by:hidrau
ID: 16488373
I got the error on it
It was at this line IF DATEDIFF(SECOND, @Dt1, @Dt2) / 3600 = 0
It must be this way IF DATEDIFF(SECOND, @Dt1, @Dt2) / 60 = 0

Even though Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

810 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