Solved

How do I query for blank spaces when I don't know how many blank spaces could exist

Posted on 2011-09-23
14
361 Views
Last Modified: 2012-05-12
I am trying to find out how many records hae a blank value in a field. I tried the following query because I don't know how many blank spaces could exist

Select count(*) from bib_holdings where publication_date like ' % ';

Is there a more accurate query that would pick all the records that would have any number of blank spaces in the publication_date field?
0
Comment
Question by:sikyala
14 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 300 total points
ID: 36588418
where publication_date like '% %';
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 300 total points
ID: 36588421
where regexp_like(publication_date,' ')
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 300 total points
ID: 36588423
where replace(publication_date,' ') != publication_date
or (replace(publication_date,' ')  is null and publication_date is not null)
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36588435
your original where clause would only count rows that begin and end with a space

the three above will count rows that have at least one space anywhere in the string
0
 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 100 total points
ID: 36588583
My personal choice would be:

WHERE INSTR(PUBLICATION_DATE, ' ') > 0
0
 
LVL 15

Assisted Solution

by:x77
x77 earned 100 total points
ID: 36589556
To find records with 3+ Spaces:

select *  
from YourTable
where (length(YourFld)-length(replace(YourFld,' ','')))>2;
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36589567
http:#36589556  doesn't handle nulls properly

for instance,  if YourFld= '         ',  it will fail


see  http:#36588423
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:sikyala
ID: 36589651
yes i just realized that because the field has a date datatype it won't allow blank spaces so my queries are accurate when they return no rows
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36589667
>> field has a date datatype

if publication_date is really a DATE  then this condition doesn't make any sense

   publication_date like ' % ';

It's legal syntax because you'll get an implicit conversion from date to string but it's not helpful
it simply adds extra work to your query for no functional gain at all
0
 

Author Comment

by:sikyala
ID: 36589681
i didn't realize that it wasn't possible until after I asked the question and tried different tests including updating a record with a blank space in the publication_date field
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36589727
not only is the condition not helpful,  it can even be misleading,
again, because of implicit conversions


SQL> create table bib_holdings(publication_date date);

Table created.

SQL> insert into bib_holdings values (sysdate);

1 row created.

SQL> insert into bib_holdings values (sysdate-1);

1 row created.

SQL> insert into bib_holdings values (sysdate+1);

1 row created.

SQL> commit;

Commit complete.

SQL> Select count(*) from bib_holdings where publication_date like ' % ';

  COUNT(*)
----------
         3

SQL>

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36589764
curious,  why was http:#36589556  chosen?  it does not work reliably to find spaces

please don't score with  "everybody wins",
you should only accept the posts that are helpful toward the question you asked
0
 

Author Comment

by:sikyala
ID: 36590418
I calculated a count of records with a value and records with a null value and total number of records that exist in the table. The number with a value plus the number with a null value equaled the total value in the table. The queries offered did work and returned no results. I used them all so I assigned points to them
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36590987
well I guess given that your specific situation was quirky (using implicit conversion on a date to check for string values)  then I guess that query "could" have worked.

but, it's not a valid solution for your question in general.
in fact, it's not even reliable for your specific situation  - it might have worked for your session, but isn't guaranteed to work in general

SQL> create table bib_holdings(publication_date date);

Table created.

SQL> insert into bib_holdings values (sysdate);

1 row created.

SQL> insert into bib_holdings values (sysdate-1);

1 row created.

SQL> insert into bib_holdings values (sysdate+1);

1 row created.

SQL> commit;

Commit complete.

SQL> Select count(*) from bib_holdings where publication_date like ' % ';

  COUNT(*)
----------
         3

SQL> select * from bib_holdings where (length(publication_date)-length(replace(publication_date,' ','')))>2;

no rows selected

SQL>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
File generation using utl_file 4 46
report returning null 21 80
ORA-00972: identifier is too long from Unix but not from Windows in SQLPLUS 17 47
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

895 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

14 Experts available now in Live!

Get 1:1 Help Now