• Iulian -207023 (10/21/2010)


    What about storing data as int like this yyyymmdd? (year*10000 + month*100 + day)

    The pros are:

    the numerical order is the same as calendar order

    and can be converted with CONVERT(datetime, x, 112) if you need to use dateadd and datediff functions for example

    What are the pros and cons of this way of storing date as int yyyymmdd.

    Thanks,

    Iulian

    The pros and cons are similar to the ones you will be having when store it in a VARCHAR datatype

    I am not sure about any pros, but there are some cons to this approach.

    One thing being How will you stop the user from entering the date in any incorrect format?

    You might want the user to store 03-February-2010 as 20100203 but the user may make a mistake and enter it as 20100302, which would become 02-March-2010 according to your assumption. These sort of issues will be almost impossible to rectify.

    And all this trouble, when you have a datatype specially designed for storing dates. So its simply not advisable to store dates in any other datatype other than DATETIME, SMALLDATETIME or some new ones in SQL Server 2008.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/