Function that Converts Integer Data to Date in SQL Server

Hello everyone,

In this article, I will try to give information about the function that converts Integer data to date in SQL Server.

In SQL Server, in some cases, you may want to convert Integer data to date.

You can easily do this using the function below.

CREATE FUNCTION INTtoDATE
(
    @LDATE INT
)
RETURNS DATETIME
AS
BEGIN

    DECLARE @DATE DATETIME,
            @DD INT,
            @MM INT,
            @YYYY INT,
            @DATESTR VARCHAR(12);

    SELECT @DD = ((@LDATE % 65536) / 256);

    SELECT @MM = (@LDATE % 65536) % 256;

    SELECT @YYYY = (@LDATE / 65536);

    SELECT @DATESTR = CAST(@DD AS VARCHAR(2)) + '-' + CAST(@MM AS VARCHAR(2)) + '-' + CAST(@YYYY AS VARCHAR(4));

    SELECT @DATE = CONVERT(DATETIME, @DATESTR, 102);

    RETURN (@DATE);

END;
GO

--Using the Function

SELECT dbo.INTtoDATE(190054667);

When you run the above function, you will see a result similar to the one below.

Function that Converts Integer Data to Date in SQL Server

As you can see, Integer data has been converted to date.

Good luck to everyone in business and life.

104 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!