473,815 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference Between NULL and Blank in SQL

Hi,

My question is, is there any difference between a NULL and a Blank
(Unknown, Not Applicable) field in MS SQL or are they the same?

Awaiting your comments,
Regards

Mar 1 '06 #1
4 32742
Am 1 Mar 2006 01:57:11 -0800 schrieb Shwetabh:
Hi,

My question is, is there any difference between a NULL and a Blank
(Unknown, Not Applicable) field in MS SQL or are they the same?

Awaiting your comments,
Regards


Yes, a very big difference! Be carefully if you have NULL valued fields. If
you do a compare and one or both are NULL, then the result is always NULL,
never true or false. Even comparing two fields which are both NULL will
give NULL as result, not true! Or if you have something like "select
sum(field) from ..." and one or more are NULL, then the result will be
NULL. Use always "if field is NULL ..." for NULL checking and for safety
maybe something like "select sum( IsNull(field,0) ) from ...". Check the
function ISNULL() in the manual.

bye,
Helmut
Mar 1 '06 #2
I thought aggreators like SUM ignored nulls...

Mar 1 '06 #3
Please see some corrections inline...

helmut woess wrote:

Am 1 Mar 2006 01:57:11 -0800 schrieb Shwetabh:
Hi,

My question is, is there any difference between a NULL and a Blank
(Unknown, Not Applicable) field in MS SQL or are they the same?

Awaiting your comments,
Regards
Yes, a very big difference! Be carefully if you have NULL valued fields. If
you do a compare and one or both are NULL, then the result is always NULL,
never true or false.


No, the comparison "<somevalue > = NULL" will result in UNKNOWN. If the
predicate is part of the WHERE clause, then the row is removed from the
result. If the predicate is part of a CHECK constraint, then the row is
allowed.
Even comparing two fields which are both NULL will
give NULL as result, not true!
The result of the comparison "NULL = NULL" also results in UNKNOWN.
Or if you have something like "select
sum(field) from ..." and one or more are NULL, then the result will be
NULL.
NULL values are excluded from aggregates. If one or more NULL values are
encountered, SQL Server will issue a warning stating that these rows are
disregarded. The only exception is the aggregate COUNT(*)
Use always "if field is NULL ..." for NULL checking and for safety
maybe something like "select sum( IsNull(field,0) ) from ...".
This only good advice if you want a NULL row to be treated as 0 in an
aggregation (for example the calculation of an average).
Check the function ISNULL() in the manual.

bye,
Helmut


In addition to Helmut's warnings, note that NULLs are promoted in
expressions. So if you write SELECT A + B AS sum_of_A_and_B and either A
or B is NULL, then sum_of_A_and_B will be NULL.

HTH,
Gert-Jan
Mar 1 '06 #4
Shwetabh (sh**********@g mail.com) writes:
My question is, is there any difference between a NULL and a Blank
(Unknown, Not Applicable) field in MS SQL or are they the same?


As said in other posts, they are not. I just like to add one thing:

NULL stands for "unknown, not appliable". An empty string, is very much
a defined value as far as SQL is concerned.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
58415
by: Mark Davenport | last post by:
Hi, Here's my question: How do I pass a NULL value in a variable to a MySQL DB? Here's an overview of my problem: I have a PHP page that processes code, then inserts the code into a database. Very straightforward. But, some NULL values are being inserted as a blank space, or the string "NULL" instead of a true NULL.
1
10114
by: Scott | last post by:
In the result of a SELECT statement, how can you change cell values from NULL to BLANK? The following does NOT do it: SET fieldname = ' ' WHERE fieldname IS NULL Also, for colums with a DATE data type, I want to change 0000-00-00 to BLANK. For for colums with a numeric data type such as DOUBLE, I want to
7
5549
by: BlueDragon | last post by:
I don't know enough math to demonstrate that any numerical operation with a null should yield a null; although I would guess that it's true. I just don't buy it, however, when dealing with strings and nulls. In a simple table with first, middle and last name columns, I would infer that a null value in the middle name column means the HR person forgot to ask. A zero length string, however, tells me HR did ask and there is no middle name....
3
9574
by: Paul T. Rong | last post by:
Do "" and Null both mean nothing?¡¡ If I don't type anything in text box, the its value is Null£¿¡¡Or it is ¡°¡±£¿ I don¡¯ think they are the same, but I don¡¯t know their difference. Thanks.
5
2157
by: tshad | last post by:
I have the following code: *************************************************************************** Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position (client,dateposted) VALUES ( @client,@dateposted)" Dim objCmd as New SqlCommand(CommandText,objConn) with objCmd.Parameters .Add("@client",SqlDbType.Char,50).value = "the companies next test" .Add("@dateposted",SqlDbType.datetime).value = dateposted.text
17
2382
by: NuB | last post by:
I have a sql query that is doing an update of records, how can I add NULL to the field in the database if the field on my screen is blank? example: I have 5 textboxes, and a user can leave some blank, delete data from a text box then hit update, how can I have NULL inserted into the field on the database instead of having a blank record in the db
7
10999
by: eddie.holder | last post by:
Hi ladies and gents. I'm hoping anyone will be able to help me in a small access problem I am having. Let me try to explain: I have a form with textboxes which I use as criteria for a query. The form is used to allow my users to search for specific company names or if left blank, all the company names, so I built my criteria as follows: Like .. & chr(42) The form works ok if I am looking for a spesific company name, but when left...
5
2711
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. I imported a table from an Access database which has some date fields. Many of these dates have never been filled in, so are nulls. If I look directly into the table data from Visual Studio it shows NULL. However, when I scroll from record to...
9
3279
by: GotDotNet? | last post by:
I have a dataset and I have to loop through it and some of the values for an insertition into the db. Some of the fields are integers and booleans but contain a NULL in the field. how can I check for a NULL and if its NULL insert DBNull into the db? Currently I'm getting error message System.DBNull.System.IConvertible.ToBoolean() now i need to get this field and insert a NULL into the db is the values
0
9610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10670
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.