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
 General SQL Server Forums
 New to SQL Server Programming
 Default Value

Author  Topic 

onkabetse
Starting Member

5 Posts

Posted - 2011-04-26 : 10:01:38
How can I validate a column to accept the current date as the default date? I have already validated the same column (orderdate) to accept date less than the current date and has worked well using (([orderdate] < GETDATE ()))

If the orderdate is not entered, the current date should be taken as the default date.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-26 : 10:05:07
A few ways

you can create a default

create table a
(
dte datetime default getdate()
)

now if you insert into the table and don't specify a date it will use getdate().

You could use a trigger to set the date on an insert

You could use getdate() in the insert statement


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-26 : 11:03:48
In addition to doing what Nigel suggested, you may want to make one other change, rra. That change is to amend the CHECK constraint to (([orderdate] <= GETDATE ())). Without that, the default value (which is getdate()) will violate the CHECK constraint.
Go to Top of Page
   

- Advertisement -