How to convert INT to Date and Date to INT
I recently switched from DBA to Database Analyst working on Data Warehouse on SQL 2012.One of the few challenges I run into is to convert date to INT for matchup in SSIS packages. I am writing this blog so I can reference it whenever I am in doubt.
Just remember that INT and DATE Functions cannot be converted directly. They need to converted o VARCHAR or CHAR.
Here is the info for reference
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Converting DATE to INT
Declare @DateConvert DATE = '12/10/2104'
SELECT CAST(CONVERT(VARCHAR(8), @DateConvert, 112) AS INT)
Converting INT to DATE
Declare @INTConvert INT = 20141012
SELECT CAST(CONVERT(VARCHAR(8), @INTConvert, 112) AS DATE)
In the statements above
- VARCHAR or CHAR can be used. I like using varchar but that my personal preference
- 112 - That is the data type format for ISO Standard. If you prefer to use US or other format, reference the Microsoft article and it should help you.
Have a great day
No comments:
Post a Comment