-- =============================================
-- Author: Simphiwe Mabaso
-- Create date: 05 July 2022
-- Description: When called this function returns a percentage
-- =============================================
ALTER FUNCTION [dbo].[CalculatePercentage]
(
@Value DECIMAL(18, 2),
@Total DECIMAL(18, 2)
)
RETURNS INT
AS
BEGIN
-- Declare the return variable here
DECLARE @Percentage AS DECIMAL(18, 2);
-- Add the T-SQL statements to compute the return value here
SET @Percentage = @Value / @Total * 100;
-- Return the result of the function
RETURN @Percentage;
END
------------------------------------------------------
--Testing the function
DECLARE @Percent DECIMAL(18, 7),
@PercentageInString NVARCHAR(50);
SET @Percent = dbo.CalculatePercentage(92, 45);
SET @PercentageInString = CONVERT(NVARCHAR(50), @Percent) + '%';
SELECT @PercentageInString [Percentage];