Solved

MS SQL 2012 SELECT without "empty" records

Posted on 2013-11-19
7
218 Views
Last Modified: 2013-11-24
Hi

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:

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

or

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

or

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?

Thanks
Sarah
0
Comment
Question by:ScuzzyJo
7 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
why not try counting to help narrow down the problem?

select
  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?
0
 

Accepted Solution

by:
ScuzzyJo earned 0 total points
Comment Utility
Hi Paul

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

SELECT *
INTO T_1213_OnlySSN
FROM T_1213_SSN
WHERE student_support_number != ''

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

Thanks
Sarah
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
!=

<>

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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 11

Expert Comment

by:Louis01
Comment Utility
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:
SELECT *
INTO dbo.T_1213_OnlySSN
FROM T_1213_SSN
WHERE IsNull(student_support_number, '') != ''

Open in new window


NO points please, NO objections to Q being closed.
0
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
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.
0
 

Author Comment

by:ScuzzyJo
Comment Utility
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.

Thanks
Sarah
0
 

Author Closing Comment

by:ScuzzyJo
Comment Utility
Resolved this with the help of someone else.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now