
USE [Darmangah]
Go



declare @old_name varchar(200)
IF EXISTS (SELECT name FROM sysobjects
     WHERE name = 'FN_MinutesToDuration' AND type = 'FN')
	 begin
	 set @old_name='FN_MinutesToDuration'+'_old_'+[dbo].[Fn_Now_DateTime]()
	  EXEC sp_rename 'FN_MinutesToDuration',@old_name;
   end
Go
--------------
create FUNCTION [dbo].[FN_MinutesToDuration]
(
    @minutes int 
)
RETURNS char(10)

AS
BEGIN
declare @hours  nvarchar(20)

SET @hours = 
    CASE WHEN @minutes >= 60 THEN
        (SELECT CAST((@minutes / 60) AS VARCHAR(2)) + ' H ' +  
                CASE WHEN (@minutes % 60) > 0 THEN
                    CAST((@minutes % 60) AS VARCHAR(2)) + ' M'
                ELSE
                    ''
                END)
    ELSE 
        CAST((@minutes % 60) AS VARCHAR(2)) + ' M'
    END

if @hours='0 M'
set @hours=''

return @hours
END
GO


