6

I wanted to skip blank data in MySQL.

My sample query is:

SELECT id, name, date from sample where name IS NOT NULL;

Sample table:

id     name         date
1                  24-04-2012
2      abc         23-04-2012

Now if I fire above query, it gives me both record but I want to skip data which is stored as blank i.e. There is nothing(not even NULL)?

So how can I skip 1st record? What should be my query?

So how to skip blank data in MySQL?

Please guide me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Prat
  • 519
  • 5
  • 16
  • 33

3 Answers3

19

You can eliminate both NULL and empty/blank strings from your results using the following:

 where name IS NOT NULL AND name <> ''
                        ^^^^^^^^^^^^^^ add this

Demo: http://www.sqlfiddle.com/#!2/1155a/6

Edit: As pointed out in the comments, trim is not even necessary.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
2

This question is already solved by mellamokb, but here I propose a one way step based on "How to check if a parameter is null or empty sql" and NULLIF mySQL function:

where  nullif( trim(name) , '') is NULL
Community
  • 1
  • 1
dani herrera
  • 48,760
  • 8
  • 117
  • 177
0
SELECT id, name, date from sample where trim(name)
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44