Guest User

Untitled

a guest
Jun 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. @start_date = '2009-04-15 10:24:00.000'
  2. @end_date = '2009-04-16 19:43:01.000'
  3.  
  4. DATEDIFF(minute, @start_date, @end_date)
  5.  
  6. CONVERT(varchar,(@end_date-@start_date),108)
  7.  
  8. DECLARE @ElapsedS INT
  9. SET @ElapsedS = DATEDIFF(second, @start_date, @end_date)
  10.  
  11. CREATE FUNCTION [dbo].[udfTimeSpanFromSeconds]
  12. (
  13. @Seconds int
  14. )
  15. RETURNS varchar(15)
  16. AS
  17. BEGIN
  18. DECLARE
  19. --Variable to hold our result
  20. @DHMS varchar(15)
  21. --Integers for doing the math
  22. , @Days int --Integer days
  23. , @Hours int --Integer hours
  24. , @Minutes int --Integer minutes
  25. --Strings for providing the display
  26. , @sDays varchar(5) --String days
  27. , @sHours varchar(2) --String hours
  28. , @sMinutes varchar(2) --String minutes
  29. , @sSeconds varchar(2) --String seconds
  30.  
  31. --Get the values using modulos where appropriate
  32. SET @Hours = @Seconds/3600
  33. SET @Minutes = (@Seconds % 3600) /60
  34. SET @Seconds = (@Seconds % 3600) % 60
  35.  
  36. --If we have 24 or more hours, split the @Hours value into days and hours
  37. IF @Hours > 23
  38. BEGIN
  39. SET @Days = @Hours/24
  40. SET @Hours = (@Hours % 24)
  41. END
  42. ELSE
  43. BEGIN
  44. SET @Days = 0
  45. END
  46.  
  47. --Now render the whole thing as string values for display
  48. SET @sDays = convert(varchar, @Days)
  49. SET @sHours = RIGHT('0' + convert(varchar, @Hours), 2)
  50. SET @sMinutes = RIGHT('0' + convert(varchar, @Minutes), 2)
  51. SET @sSeconds = RIGHT('0' + convert(varchar, @Seconds), 2)
  52.  
  53. --Concatenate, concatenate, concatenate
  54. SET @DHMS = @sDays + ':' + @sHours + ':' + @sMinutes + ':' + @sSeconds
  55.  
  56. RETURN @DHMS
  57.  
  58. END
  59.  
  60. SELECT TimeSpan = dbo.udfTimeSpanFromSeconds(@ElapsedS)
Add Comment
Please, Sign In to add comment