error in default date

  • 1. Create table say table1 with column col1 as varchar(2)

    and col2 as datetime and add default constraint to col2 as getdate()

    2. Insert into table1 ( col1,col2) values('a') - this will insert default getate()

    3. Insert into table1 ( col1,col2) values('a','') - this will insert default datetime as '1900-01-01 00:00:000'

    Why in 3. is not taking default getdate()?

  • rpatil22 (12/16/2009)


    1. Create table say table1 with column col1 as varchar(2)

    and col2 as datetime and add default constraint to col2 as getdate()

    2. Insert into table1 ( col1,col2) values('a') - this will insert default getate()

    3. Insert into table1 ( col1,col2) values('a','') - this will insert default datetime as '1900-01-01 00:00:000'

    Why in 3. is not taking default getdate()?

    Because in #3 you are passing an empty string that actually will convert to an integer 0 which will convert to the date 1900-01-01. This means you are entering a date, so no default.

  • a default, any default constraint in fact, uses the value ONLY if the column is not referenced in the INSERT statement. the empty string you included gets assumed to be the date.min, which is 1900/01/01 i think.

    the same will happen with any other constraint type.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • It may be worth to mention also that varchar(2) is a bad design choice. because in fact up to 4 bytes are required to store the value.

    Regards

    Piotr

    ...and your only reply is slàinte mhath

Viewing 4 posts - 1 through 3 (of 3 total)

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