Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Current time as a default value

Author  Topic 

rafael.soteldo
Starting Member

36 Posts

Posted - 2012-09-26 : 20:29:00
I have a DateTime field in a table, how can I set the current date/time as a default value for this field?

Thanks in advance for your time

-------
Rafael Soteldo

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-26 : 20:33:00
[code]ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
DEFAULT getdate()
FOR <column name>[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

rafael.soteldo
Starting Member

36 Posts

Posted - 2012-09-26 : 21:32:27
Thank you.

And how do I set it when designing the table?, I wrote getdate() in "Default value or binding" and I got an error message.

By the way, how do I set your answer as the correct answer as in other forums?

Thanks in advance for your time

-------
Rafael Soteldo
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-26 : 21:55:43
quote:
And how do I set it when designing the table?, I wrote getdate() in "Default value or binding" and I got an error message.

what is the error message ?

if you are creating the table via CREATE TABLE command

CREATE TABLE <table name>
(
<column name> datetime DEFAULT getdate()
)


quote:
By the way, how do I set your answer as the correct answer as in other forums?

this forum software does not has this feature


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

rafael.soteldo
Starting Member

36 Posts

Posted - 2012-09-27 : 13:27:35
I'm using the visual designer that comes with SQL Server Management Studio 2008.

Can I use this function getdate() in the designer, or do I have to do it via sql code?

Thanks in advance for your time

-------
Rafael Soteldo
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-09-28 : 08:54:08
in SSMS, when you use the designer, there is a column properties tab that you can use to input a default value.








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

rafael.soteldo
Starting Member

36 Posts

Posted - 2012-09-28 : 22:19:29
Thank you

Thanks in advance for your time

-------
Rafael Soteldo
Go to Top of Page
   

- Advertisement -