CREATE FUNCTION FitSpace (@kbsize INT) RETURNS NVARCHAR(200) AS BEGIN DECLARE @rtn NVARCHAR(200); DECLARE @gb INT=1000000, @mb INT=1000, @kb INT=1; SELECT @rtn=CASE WHEN @kbsize>=@gb THEN CONCAT(CAST(ROUND(@kbsize*1.0/@gb,3) AS FLOAT),'GB') WHEN @kbsize<@gb AND @kbsize>=@mb THEN CONCAT(CAST(ROUND(@kbsize*1.0/@mb,3) AS FLOAT),'MB') WHEN @mb>=@kbsize THEN CONCAT(@kbsize,'KB') END; RETURN @rtn; END