How to calculate percentage using function in ms sql server
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[CalculatePercentage]
(
-- Add the parameters for the function here
@Obtain AS INTEGER = 0,
@All AS INTEGER = 0
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @percentage as decimal(18,2)=0;
set @percentage=cast(@Obtain/(convert(decimal(18,2),@All)) * 100 as int )
return @percentage
END
Caculate percentage
show percentage in ms sql server.
Share This with your friend by choosing any social account