Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem in Default constraint

Author  Topic 

nishu81
Starting Member

11 Posts

Posted - 2009-11-26 : 08:06:02
Hi
I am facing two problem while working with Default constraint

My table "shippers" includes 3 columns: "Shippers ID(identity colmn) ","shippersName","ShippedDate"

Now I have Provided default constraints on "Shippersname" with Default value ashok and for "shippeddate" as getdate()
Note that all the column are not null

This constraints is working fine when I am using a query like

insert into shippers (shippersName) values ('nishu')

or
insert into shippers (shippeddate) values ('01/01/2008')

In first case since i have not provided shippeddate so it is taking my defualt value getdate() and in second I have not provided shippersName so it is taking my default value <ashok>

My Problem is when I am using a query like
insert into shippers (shippersName, shippeddate) values ('','')

Then in above case also I am not providing any value so it should dake my default value but what is happening is shippeddate is taking default value of SQL server 1900-01-01 00:00:00.000 and shippersName is blank.It is not even NULL
I want to ask 2 questions

1)when i am using '' for varchar .Is it working as some value?isn't equal to null
2)why when I am using '' as for dattime it is not taking my default date and rather taking SQL default date

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-26 : 08:35:47
If you want default value to be taken by the column, you should omit it from insert statement
Refer this to know why you should not assign '' for datetime columns
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/02/empty-string-and-default-values.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -