December 16, 2009 at 4:15 pm
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()?
December 16, 2009 at 4:36 pm
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.
December 16, 2009 at 4:36 pm
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
December 16, 2009 at 5:55 pm
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