tags:

views:

10731

answers:

5

Using Transact SQL is there a way to specify a default datetime on a column (in the create table statement) such that the datetime is the minimum possible value for datetime values?

create table atable
(
  atableID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
  Modified datetime DEFAULT XXXXX??????
)

Perhaps I should just leave it null.

+3  A: 

"Perhaps I should leave it null"

Don't use magic numbers - it's bad practice - if you don't have a value leave it null

Otherwise if you really want a default date - use one of the other techniques posted to set a default date

DJ
I would tend to agree. As an aside, do you have an answer to the question?
I hate when users attempt to redirect the user instead of answering and -then- trying to redirect.
TheTXI
"Perhaps I should just leave it null." - I was answering that part
DJ
Q: has it been modified?A: no, you are inserting the value.I keep a CreatedDate and a ModifiedDate on my data. ModifiedDate is always NULL on insert. It's set by trigger on UPDATE.
Quango
A: 

I think this would work...

create table atable
(
  atableID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
  Modified datetime DEFAULT ((0))
)

Edit: This is wrong...The minimum SQL DateTime Value is 1/1/1753. My solution provides a datetime = 1/1/1900 00:00:00. Other answers have the correct minimum date...

Jason Punyon
+1. Doing WHERE Modified = 0 is SARGable, WHERE Modified IS NULL isn't.
Tomalak
this won't work - 0 is '1/1/1900'. -53690 would give you the value of '1/1/1753'.
Scott Ivey
Hm. Maybe I don't get it, but what's '1/1/1753'? And why won't '1/1/1900' work?
Tomalak
1/1/1900 would work, but is just not the minimum, which is what OP was after. 1/1/1753 is the last date before the calendar goes nuts (with diff calendars adopted). there are methods for storing dates, but not using DateTime. I think SQL08 introduced a new type (DateTime2?) that handles it
Chris
+3  A: 

As far as I am aware no function exists to return this, you will have to hard set it.

Attempting to cast from values such as 0 to get a minimum date will default to 01-01-1900.

As suggested previously best left set to NULL (and use ISNULL when reading if you need to), or if you are worried about setting it correctly you could even set a trigger on the table to set your modified date on edits.

If you have your heart set on getting the minimum possible date then:

create table atable
(
  atableID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
  Modified datetime DEFAULT '1753-01-01'
)

Chris
A: 

I think your only option here is a constant. With that said - don't use it - stick with nulls instead of bogus dates.

create table atable
(
  atableID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
  Modified datetime DEFAULT '1/1/1753'
)
Scott Ivey
+1  A: 

Unless you are doing a DB to track historical times more than a century ago, using

Modified datetime DEFAULT ((0)) 

is perfecly safe and sound and allows more elegant queries than '1753-01-01' and more efficientt queries than NULL.

However, since first Modofied datetime is the time at which the record was inserted, you can use:

Modified datetime NOT NULL DEFAULT (GETUTCDATE())

which avoids the whole issue and makes your inserts easier and safer - as in you don't insert it at all and SQL does the housework :-)

With that in place you can still have elegant and fast queries by using 0 as a practical minimum since it's guranteed to always be lower than any insert-generated GETUTCDATE().

ZXX