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,438
  • 68
  • 55
  • 126
Prat
  • 509
  • 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
  • 55,194
  • 12
  • 105
  • 134
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
  • 44,444
  • 7
  • 103
  • 165
0
SELECT id, name, date from sample where trim(name)
Robin Castlin
  • 10,878
  • 1
  • 25
  • 43