Sunday, February 19, 2012

default value in datetime column

hi, i was wondering how to set default value in the datetime column of the database so that it will enter current date and time if one is not provided when a row is populated. is there a store procedure to do this? or built-in function?

mp

If you go to Modify view of the table in either Enterprise Manager or SQL Server Management Studio and select that database field, there should be a property where you can specify the default value. Enter GETDATE() as the default value.|||

There are 2 ways to add default value as Today's Date

1) Using QueryAnalyzer

alter table <TableName>
add <ColumnName> DateTime default getdate()

//This will add one column with Default value as Today's date. If you want to add to existing column...2)

2) go to Enterprise Manager, ... , select your table, Right click, go to Design Table and open...

place the mouse cusor in which column you want to put default value, When you place the mouse cursor bottom of this you will see Columns Tab-->Default value TextBox put getdate() and Save . Now insert any row it will automatically enters to days date

|||

thanks, just what i needed!

Happy New Year!!!

mp

No comments:

Post a Comment