I am adding some tables to a data warehouse and want to store datetime information into an int field (the time part is not important to me)
Note that I'm not storing as 20150123, I just store it as pure integer using
CAST(field as int)
that stores 2015-06-18 12:57:47.833 as 42172
I can easily add a day by +1 and add a week by adding 7 to the field. however adding a month is not straight forward. Also whenever I need a date representation of the data, I can cast it to datetime.
Just want to know what pros and cons you see on this?
Converting between an
intand adatetimeworks just fine. It's well defined what the conversion does, so there is nothing mysterious going on that might suddenly change or stop working.Let's look at some aspects:
intand it's sorted by date.datetimeto make any sense of it.You can for example do the same conversion from
inttoDateTimein C#:When it comes to storage space, there isn't really any advantage. An
intuses four bytes and adatetimeuses eight bytes. If you really need to preserve space, you should rather use thedatetype, which uses only three bytes.