convert mb to gb in sql server

Convert MB to GB in SQL Server

Hello everyone, I m going to share the query for convert MB to GM in sql server.

The Query as given below.

ALTER FUNCTION [DBO].[FUN_CONVERTMBTOGB]
(
            @USAGEINCLUSION BIGINT
)
RETURNS VARCHAR(50)
AS
BEGIN
            DECLARE @RETURN VARCHAR(50)
            DECLARE @ISRESULT VARCHAR(50)
            DECLARE @TGB INT
           
            IF(@USAGEINCLUSION >= 1024)
            BEGIN
                        SET @ISRESULT =(SELECT  CAST(@USAGEINCLUSION AS DECIMAL(18,2))/1024 AS INT)
                        SET @TGB = (SELECT VALUE FROM  [DBO].[SPLIT](@ISRESULT, '.') WHERE ID= 2)
                        IF @TGB > 0
                                     BEGIN                        
                                                SET @RETURN = CAST(FORMAT(CAST(@ISRESULT AS DECIMAL(18,2)),'N2') AS NVARCHAR(15)) + ' GB';
                                     END
                        ELSE
                                     BEGIN                        
                                                SET @RETURN =(SELECT VALUE FROM  [DBO].[SPLIT](@ISRESULT, '.') WHERE ID=1) + ' GB'
                                     END
            END
            ELSE
                        BEGIN
                                    SET @RETURN = CAST(@USAGEINCLUSION  AS NVARCHAR(15)) + ' MB';                     
                        END

            RETURN @RETURN


END




ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.