Saturday, February 25, 2012

Returning Null values

I have a simple table on my webpage which shows data from my database.

It all worked fine apart from one part. I wanted the select statement to select only NULL values from a column, as these are classed as open queries. Once I have closed the query a 0 or 1 will automatically be posted and will no longer have a NULL value.

I have a simple select statement (which I will post on here when I log in with my work computer) and at the end I have a WHERE Column = NULL. I have also tried WHERE column <> 0.0 AND column <>1.0 and that did not work.

If I only did WHERE column <> 1.0, i only get the 0.0 results and none of the NULL results, but if I have no WHERE statement I get all the results including the NULL values.

Oliver

What is the data type of coloumn?

|||

I think it is decimal.

|||

Try

where column is null

It will give you all rows that have null in column field

|||

Hi, thanks for your tip. I had tried this before, and had no luck.

Any other ideas?

|||

Any "other ideas" is just a guessing game and will only take that much longer for anyone to guess an "idea" that will actually solve your problem. Instead, why dont you post your table structure, some sample data and the expected output so the ideas can be more focussed..Smile

|||

As Prashant points out, "where column is null " is the correct syntax. If you think you tried it but it didn't work, you probably didn't do what you think you did.

BTW, as a rule, it's better to set a value in all cases and not use nulls (unless you want to represent a missing foreign key). The reason is that WHERE clauses behave in ways that areunintuitive with nulls, for example, the reason "WHERE column <> 1.0" did not return any of your nulls is because null means unknown, not unpopulated, and a column that us unknown might contain a 1.0. I know that will strike you as wierd, but that's they way all databases work AFAIK, it's not just a sql server thing. So, try to make your columns non-nullable, and if you're adding a column, in which case it must be nullable, bind it to a default and then issue an update all existing rows to set it to the default value

No comments:

Post a Comment