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 2000 Forums
 SQL Server Development (2000)
 Set up default value for today's day

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-05-12 : 15:15:20
How to set up default value in a OrderDate field as today's date? So that if new data insert into table, OrderDate will auto fill today's date?

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-05-12 : 15:16:53
I got it. Add getdate
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-05-12 : 15:19:44
ALTER TABLE YourTable ADD CONSTRAINT def_YourColumn (or whatever you want to name it) DEFAULT GETDATE() FOR YourColumn WITH VALUES
GO

You can check out ALTER TABLE in BOL for more details.


Tara Kizer
aka tduggan
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-12 : 15:19:59
Put getdate() in the Default Value

Srinika
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-14 : 10:17:31
use dateadd(day, datediff(day, 0, getdate()), 0) if you want default to today's date without the time (or time = 00:00:00)


KH

Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-05-14 : 10:54:25
CURRENT_TIMESTAMP

Important that the column does not allow null, otherwise the default will not bite.
nullable column, you need to do this:
insert orders(...,OrderDate) values(...,default)
not null:
insert orders(...) values(...)

rockmoose
Go to Top of Page
   

- Advertisement -