locked
getdate function RRS feed

  • Question

  • Hi,
     
    In  Access when declaring  a new date field it is possible to set the default date value using =Date().

    In SQL Server this can be done using getdate().

    However in Access it also possible to set the default date to be 7 days after the current day by using =Date()+7.

    What is the best practice to do the same thing in Sql Server? Should I set the default to getdate()+7

    I hope this is understandable and thanks in advance for any help.

    Regards

    Andrew
    Software Developer
    Tuesday, January 6, 2009 10:57 AM

Answers

  •  Yes, while this works it is not recommended to use this implicit way. Although the (+) is "overloaded to add days to the current date from GETDATE(), it is better to use the explicit function DATEADD(dd,+1,GETDATE()) as it will allow you to manipulate the date using different timespans like months (m), milliseconds (ms) etc. Another reason to get use to this functiton is that SQL Server 2008 and the new (date) data types will not support the implicit way and will only allow you to tweak the date using the Date functions.

    -Jens K. Suessmeyer
    • Marked as answer by Andrew Mercer Tuesday, January 6, 2009 3:33 PM
    Tuesday, January 6, 2009 12:39 PM

All replies

  •  Yes, while this works it is not recommended to use this implicit way. Although the (+) is "overloaded to add days to the current date from GETDATE(), it is better to use the explicit function DATEADD(dd,+1,GETDATE()) as it will allow you to manipulate the date using different timespans like months (m), milliseconds (ms) etc. Another reason to get use to this functiton is that SQL Server 2008 and the new (date) data types will not support the implicit way and will only allow you to tweak the date using the Date functions.

    -Jens K. Suessmeyer
    • Marked as answer by Andrew Mercer Tuesday, January 6, 2009 3:33 PM
    Tuesday, January 6, 2009 12:39 PM
  • Thanks

    Very helpful

    Andrew
    Software Developer
    Tuesday, January 6, 2009 3:33 PM