Glen
Glen

Reputation: 49

NOT NULL returning NULL values

When i run the following query i get 12 rows, when i should have 6 (the ones that are not null in 'firsts') any reason why iam getting all 12? Becuase they are future dates they are empty but i wanted to have the table display the dates for whole year on the table. i have used varchar and not int for the firsts which isnt good practice i know.

SELECT * 
FROM office_figures 2016 
WHERE date_figures=2017 AND firsts IS NOT NULL

mysql table

Upvotes: 0

Views: 4473

Answers (2)

Black Mamba
Black Mamba

Reputation: 15545

SELECT * 
FROM office_figures 2016 
WHERE date_figures=2017 AND firsts IS NOT NULL AND firsts !="";

You need to check for an empty value in column as well as "" != NULL.

  • Null is an absence of a value. An empty string is a value, but is just empty. Null is special to a database.

  • Null has no bounds, it can be used for string, integer, date, etc. fields in a database.

  • NULL isn't allocated any memory, the string with NUll value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is "".

Difference between NULL and Blank Value in Mysql
Check the above link to know more.

Upvotes: 1

David Dutra
David Dutra

Reputation: 401

Probably the firsts that you don't want to be there aren't null, they might be just empty strings ''. Try this:

SELECT * 
FROM office_figures 2016 
WHERE date_figures=2017 AND firsts IS NOT NULL AND firsts != ''

Upvotes: 3

Related Questions