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.
As you can see, Integer data has been converted to date.
Good luck to everyone in business and life.