Tuesday, February 14, 2012

DateAdd() in Default Value

My goal is to set the default value of a smalldatetime field to the GetDate() plus 4 hours. I figure I need to use DateAdd() for this, but can't figure out how to place it in a default value. I can put GetDate() in default value, but DateAdd(hour,4,GetDate()) throws a syntax error.

Is there a simple way to accomplish this?

bes7252,

What error are you seeing, exactly? I have no problem executing the following code:

create table t(
i int,
d datetime default (dateadd(hour,3,getdate()))
)
go

drop table t

Have you left out a ) somewhere?

Steve Kass
Drew University
http://www.stevekass.com
|||

Logically it doesn’t make any sense to have this expression on your column. Suppose if you generate the expression on your stored date then it is acceptable.

|||

Steve,

I tried it again and didn't have any problems. I don't remember the error I was seeing yesterday, but must have had a typo-o in my expression. (I typed the one on the post from memory, not copy/paste).

Thanks!

Brian

No comments:

Post a Comment