April 18, 2007 at 8:04 am
I am using SQL Server 2000. I have a Column with DataType int and default value specified as (null). But, With Insert or Update if the column value is Blank, 0 is getting inserted instead of the desired NULL.
Thanks
April 18, 2007 at 8:20 am
That's because there's a default value in the column.
Or in insert trigger that update the rows.
Or a default value has been directly coded within the insert statement.
I must add that NULL is the default default for any column that allows null, so no default should be created at all for the default to be NULL.
April 18, 2007 at 8:45 am
The Column allows NULLs and there is a default value of (null) for the column. There are no triggers. Also, There is no default value in the insert statement. I tried with '' for the column in the insert statement and still 0 is inserted instead of the desired NULL. Any other ideas please ?
Thanks
April 18, 2007 at 9:07 am
Please provide the create script for the table, and the insert statements you have tried.
April 18, 2007 at 9:35 am
I was able to resolve my problem using NULLIF. Thanks.
April 18, 2007 at 10:04 am
Please post your findings so that other members can benefit from you work.
April 18, 2007 at 11:38 am
Used NULLIF(column-name, '') and that helped in converting '' to NULL.
April 18, 2007 at 12:00 pm
If that worked, and you didn't have nulls showing up before as defaults, then something was populating them with empty strings.
April 18, 2007 at 1:43 pm
I meant the whole insert statment... I'm sure most folks smart enough to use searh engine are also smart enough to use books online .
April 19, 2007 at 6:41 am
Here is the whole Insert statement
Insert Into catalogItems (categoryId, itemPrice, supplierCompanyId, itemDeliveryDays, itemLimit) Values ('${param.category}', NULLIF('${param.itemPrice}',''), '${param.splrCompany}', '${param.delDays}', NULLIF('${param.itemLimit}',''))
itemPrice is of float datatype and itemLimit is of int datatype. If user did not enter anything for them in the form, 0 was getting inserted into the Table. Using NULLIF , <NULL> is getting inserted as desired.
Hope it helps someone out there.
-vmrao
April 19, 2007 at 7:10 am
Ok, so that was just the UI thatw as not sending the correct information to the stored procedure. You are using stored procedures right?
April 19, 2007 at 7:58 am
Based on what you have said and done I assume when you application makes the call to load the data using parameters you are adding using "" as the value which is why you ran into this issue. Instead (and depending on your programming language) you need to set the parameter to NULL directly and change only if the incoming value length is greater than 0.
Ex C#
cmdSQL.Parameters.Add(
new SqlParameter("@Manager_ID",SqlDbType.VarChar,10,ParameterDirection.Output,false,0,0,"Manager_ID",DataRowVersion.Current,null));
if (Request.Form("Manager").length > 0
cmdSQL.Parameters["@Manager_ID"].value = Request.Form("Manager").value;
April 19, 2007 at 11:11 am
I am not using any Stored Procedures.
Its because of the SQL Server behavior. If you try to put '' into a column of int datatype, SQL Server puts a default value of 0. Using NULLIF puts <NULL> instead of 0.
- vmrao
April 19, 2007 at 11:20 am
I'd be curious to test this one with the profiler, but I don't have .Net installed so looks like I'll have to pass.
AFAIK, sql server will put exactly what you tell it to put, no more, no less.
April 19, 2007 at 11:39 am
The reason you were getting a zero inserted is that '' converts to 0 when cast as an int. Try this:
select cast('' as int) -- the return value is 0
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply