MS SQL 2012 SELECT without "empty" records

Posted on 2013-11-19
Last Modified: 2013-11-24

I'm trying to SELECT into a new table where a particular column contains data which ignores the rows where a certain field is empty/null/whatever!

I've tried various ways, including the following:

INTO dbo.T_1213_OnlySSN
WHERE student_support_number IN (SELECT student_support_number FROM T_1213_SSN);


WHERE student_support_number Is Not Null OR student_support_number <> '';


WHERE student_support_number Is Not Null OR len(ltrim(student_support_number)) <> 0;

I've read around and think the field will be empty rather than Null, but none of these are working and I end with a load or rows where the student_support_number (nvarchar) has nothing in it.

Any ideas?

Question by:ScuzzyJo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 39659063
why not try counting to help narrow down the problem?

  count(*) as count_all
, count(case when student_support_number Is Null then student_support_number end) as count_null
, count(case when student_support_number = '' then student_support_number end) as count_empty
, count(case when student_support_number Is Not Null or student_support_number <> '' then student_support_number end) as count_not_null_empty
from T_1213_SSN

what is this result?

Accepted Solution

ScuzzyJo earned 0 total points
ID: 39659151
Hi Paul

Thanks for your help.  I've resolved this with the help of someone and have used:

INTO T_1213_OnlySSN
WHERE student_support_number != ''

This seems to work fine.  I'm not sure why/how, but will look into that.

LVL 48

Expert Comment

ID: 39659166


are equivalents (both do "not equal")

which you stated you had already tried...
WHERE student_support_number Is Not Null OR student_support_number <> '';

glad it's resolved.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 39659191
Hi Sarah
Just a note on your SQL above. The
WHERE student_support_number != ''
would still return NULL values. Perhaps it is safer to use this:
INTO dbo.T_1213_OnlySSN
WHERE IsNull(student_support_number, '') != ''

Open in new window

NO points please, NO objections to Q being closed.
LVL 27

Expert Comment

ID: 39659570
The problem is that you have some unprintable charatcers in your column which are not equal to '' even though it apperas empty and len(col) still is >0

In order to find thoese rows you can run:

select * from where PATINDEX('%[^0-9a-z]%', student_support_number)>0

This will return all the rows where student_support_number has anything that is not a digit between 0 and 9 or a letter between a and z. It is case insensitive.

If that query returns something you will have to clean the values in the student_support_number column.

Author Comment

ID: 39659663
Hi Guys

Louis - when I looked at the data, I realised there shouldn't be any null values.  It comes from a download from a massive student database which won't allow them.  Thanks for the comment though, I'll bear it mind as it's bound to be relevant later on.

Z - thanks for yours too.  SSN's themselves can only contain digits and alphabet characters between a and z.  I take your point though and will watch out for it in future.


Author Closing Comment

ID: 39672346
Resolved this with the help of someone else.

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replacing unrecognized function name in SQL Query 4 47
SQL Server 2012 to SQL Server 2016 24 54
Sorting a SQL script 5 39
T-SQL: need to reset a declared variable 4 31
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question