Wrote this bad boy last year for a job which returned the time between two events as a integer representing minutes. It needed to be output in an idiot-proof fashion e.g. 1 day, 6 hours, 12 minutes instead of just 1812 minutes.It only really needed to cover a period of up to a couple of days but I expanded it to be able to handle any number that can be handled by an integer data type going up to n years - call it future-proofing if you like but its just because you never do know . . .CREATE FUNCTION fn_MinutesToWords (@Minutes INT)RETURNS varchar(100)AS/*Author: Nick Holt 2010*/BEGIN--DECLARE @Minutes INT--SET @Minutes = 1501--SELECT dbo.fn_MinutesToWords (1812)DECLARE @Words VARCHAR(100)SET @Words = ''DECLARE @Hours INTDECLARE @Days INTDECLARE @Weeks INTDECLARE @Years INTIF @Minutes < 60BEGINSET @Words = CASE WHEN @Minutes = 1 THEN CAST(@Minutes AS VARCHAR) + ' minute' ELSE CAST(@Minutes AS VARCHAR) + ' minutes' ENDENDIF @Minutes BETWEEN 60 AND 1439BEGINSET @Hours = @Minutes / 60SET @Minutes = @Minutes % 60SET @Words = CASE WHEN @Hours = 1 THEN CAST(@Hours AS VARCHAR) + ' hour, ' ELSE CAST(@Hours AS VARCHAR) + ' hours, ' END + CASE WHEN @Minutes = 1 THEN CAST(@Minutes AS VARCHAR) + ' minute' ELSE CAST(@Minutes AS VARCHAR) + ' minutes' ENDENDIF @Minutes BETWEEN 1440 AND 10079BEGINSET @Days = @Minutes / 1440SET @Minutes = @Minutes % 1440SET @Hours = @Minutes / 60SET @Minutes = @Minutes % 60SET @Words = CASE WHEN @Days = 1 THEN CAST(@Days AS VARCHAR) + ' day, ' ELSE CAST(@Days AS VARCHAR) + ' days, ' END + CASE WHEN @Hours = 1 THEN CAST(@Hours AS VARCHAR) + ' hour, ' ELSE CAST(@Hours AS VARCHAR) + ' hours, ' END + CASE WHEN @Minutes = 1 THEN CAST(@Minutes AS VARCHAR) + ' minute' ELSE CAST(@Minutes AS VARCHAR) + ' minutes' ENDEND IF @Minutes BETWEEN 10080 AND 524159BEGINSET @Weeks = @Minutes / 10080SET @Minutes = @Minutes % 10080SET @Days = @Minutes / 1440SET @Minutes = @Minutes % 1440SET @Hours = @Minutes / 60SET @Minutes = @Minutes % 60SET @Words = CASE WHEN @Weeks = 1 THEN CAST(@Weeks AS VARCHAR) + ' week, ' ELSE CAST(@Weeks AS VARCHAR) + ' weeks, ' END + CASE WHEN @Days = 1 THEN CAST(@Days AS VARCHAR) + ' day, ' ELSE CAST(@Days AS VARCHAR) + ' days, ' END + CASE WHEN @Hours = 1 THEN CAST(@Hours AS VARCHAR) + ' hour, ' ELSE CAST(@Hours AS VARCHAR) + ' hours, ' END + CASE WHEN @Minutes = 1 THEN CAST(@Minutes AS VARCHAR) + ' minute' ELSE CAST(@Minutes AS VARCHAR) + ' minutes' ENDENDIF @Minutes >= 524160BEGINSET @Years = @Minutes / 524160SET @Minutes = @Minutes % 524160SET @Weeks = @Minutes / 10080SET @Minutes = @Minutes % 10080SET @Days = @Minutes / 1440SET @Minutes = @Minutes % 1440SET @Hours = @Minutes / 60SET @Minutes = @Minutes % 60SET @Words = CASE WHEN @Years = 1 THEN CAST(@Years AS VARCHAR) + ' years, ' ELSE CAST(@Years AS VARCHAR) + ' yearss, ' END + CASE WHEN @Weeks = 1 THEN CAST(@Weeks AS VARCHAR) + ' week, ' ELSE CAST(@Weeks AS VARCHAR) + ' weeks, ' END + CASE WHEN @Days = 1 THEN CAST(@Days AS VARCHAR) + ' day, ' ELSE CAST(@Days AS VARCHAR) + ' days, ' END + CASE WHEN @Hours = 1 THEN CAST(@Hours AS VARCHAR) + ' hour, ' ELSE CAST(@Hours AS VARCHAR) + ' hours, ' END + CASE WHEN @Minutes = 1 THEN CAST(@Minutes AS VARCHAR) + ' minute' ELSE CAST(@Minutes AS VARCHAR) + ' minutes' ENDENDRETURN @Words--SELECT @WordsEND
---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Clubhttp://www.hollowtreewebdesign.co.uk - a web design company in its infancy