SQLRIZ
Converting Integer to Date
Posted on February 22nd, 2016

I used to be DBA and had little exposure to development but since I switched my job and working as a Database Analyst, I have to write code extract data and create reports. One of the challenges I constantly run into is to convert date to int or int to date. I am writing this blog so I can come back and review it whenever I forget.

I used CAST and CONVERT functions a lot referenced here
​https://msdn.microsoft.com/en-us/library/ms187928.aspx


In this example, I am converting date to INT so it can be compared to keys in the Data Warehouse(DW)

​DECLARE @startdate DATE = '2015/06/01';

SELECT  CAST(CONVERT(VARCHAR(10), @startdate, 112) AS INT);



Now I am converting INT to Date

​DECLARE @startdate INT = 20150601;

SELECT  CONVERT(DATE, CAST(@startdate AS VARCHAR(10)), 112)

Just remember that INT and DATE functions cannot be converted directly.




Posted in not categorized    Tagged with no tags


0 Comments

Leave a Comment


Categories
no categories
Tags
no tags