megabytes
megabytes

Reputation: 185

Checking null values in database

I have a database with stores a sizeable chunk of binary data. A remote app which checks the database in the office.

I basically want the remote app to check that the data exists without having to 'download' it from the remote database.

SELECT BinaryData FROM DB WHERE BinaryData IS NOT NULL

would work, but it would download the data also, I just want a simple check.

Any ideas

Upvotes: 0

Views: 160

Answers (4)

milan
milan

Reputation: 133

If you are using SQL Server (2008+) you can use EXISTS which could be faster than simple COUNT(*) because it stops selecting rows on first match.

IF EXISTS (SELECT 1 FROM DB WHERE BinaryData IS NOT NULL)
    SELECT 1
ELSE SELECT 0

Alternatively you can write something like this (in this case you will get 1 if there is a match and no rows selected if there isn't)

SELECT 1 WHERE EXISTS (SELECT 1 FROM DB WHERE BinaryData IS NOT NULL)

More examples can be found in docs.

Upvotes: 0

Ajay Kaushik
Ajay Kaushik

Reputation: 101

another way for Checking not null value

SELECT COUNT(*) as Count_BinaryData FROM DB WHERE DATALENGTH(BinaryData) > 0 

Upvotes: 0

cmprogram
cmprogram

Reputation: 1884

Just don't select any data to be downloaded.

SELECT NULL FROM DB WHERE BinaryData IS NOT NULL

Upvotes: 0

Habeeb
Habeeb

Reputation: 8007

You can select the count of Not Null rows. This way you will not have to download the binary data to the remote app.

SELECT COUNT(*) FROM DB WHERE BinaryData IS NOT NULL

Upvotes: 1

Related Questions