I have a date field in my database that I would like to insert the current date on the server every time a new record is added to the table. I have a data type of “datetime” and the Default Value of Binding of “(getdate())” but it returns null, what should
I do in this case.
I’ve had a look through some Microsoft auto generated code, e.g.. the code generated for membership and the aspnet.mdf when you create a new MVC site and it looks like MS don’t rely on SQL Server adding the date time automatically. So I will do it this way
from now on.
Cheers,
Mike.
Marked as answer by Chen Yu - MSFT on Feb 27, 2012 05:56 AM
bojangles
Participant
974 Points
642 Posts
Default Date in SQL Server 2008 R2
Feb 20, 2012 02:27 AM|LINK
Hi,
I have a date field in my database that I would like to insert the current date on the server every time a new record is added to the table. I have a data type of “datetime” and the Default Value of Binding of “(getdate())” but it returns null, what should I do in this case.
Cheers,
Mike.
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Default Date in SQL Server 2008 R2
Feb 20, 2012 02:35 AM|LINK
set your default in the database, not the application.
bojangles
Participant
974 Points
642 Posts
Re: Default Date in SQL Server 2008 R2
Feb 20, 2012 02:42 AM|LINK
Thats what i'm trying to do but for some reason it's not doing it, i get null. Is there any reason you know of as to why I would get that?
Chen Yu - MS...
All-Star
21569 Points
2493 Posts
Microsoft
Re: Default Date in SQL Server 2008 R2
Feb 24, 2012 06:37 AM|LINK
Hi,
Would you please post the query when you insert getdate() record to your table? It will give us more information about your problem.
I found this thread which is talking about set date = getdate(). It may help you.
Please see : http://stackoverflow.com/questions/4672103/default-getdate-for-insert-date
http://stackoverflow.com/questions/4315295/sql-server-set-null-value-to-todays-value/4315323#4315323
Thanks.
Feedback to us
Develop and promote your apps in Windows Store
bojangles
Participant
974 Points
642 Posts
Re: Default Date in SQL Server 2008 R2
Feb 26, 2012 08:15 AM|LINK
Thanks Chen Yu,
I’ve had a look through some Microsoft auto generated code, e.g.. the code generated for membership and the aspnet.mdf when you create a new MVC site and it looks like MS don’t rely on SQL Server adding the date time automatically. So I will do it this way from now on.
Cheers,
Mike.