How do you specify default date in a smalldatetime column

  • In table designer in SQL 2000 I have a column that has a definition of smalldatetime and I want it to default to the current datetime for any row insert. What syntax do I use to default this column ?  I cannot seem to find an answer in BOL.

  • Getdate() should work like a charm. Is that giving you errors of some sort?

     

  • That is it !!! Thanks, I was drawing a blank for some reason on it....

  • No problem at all. I was going to say you can also use the niladic function CURRENT_TIMESTAMP, but it just turns it into Getdate() anyway, so might as well cut out the middleman.

  • I was toying with the idea of that myself. I used DTS to bring the table definition/data from DB2 and that is how it created it.

    Thanks for the answer. I knew it was simple, just could not think of it...

  • If you want the day only, without the time part you could use:

    convert(char(8), getdate(), 112)

    There are other solutions, but I like this one.

  • Henk, if he uses that as the default for a smalldatetime column, it will convert it right back into a date (it will lose the formatting that style 112 applied to it), including time (although the time will be midnight).

  • @david-2:

    OK, you are right.

    The smalldatetime values still  has a time part, set to 0 (00:00:00.000).

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply