Search code examples
sqlsql-serversql-server-2016

SQL Query to find all columns except purely numeric or blank values


I have a column in sql table where you can store anything (character, numeric, special character, blank space or a combination of all). I want to find or have a select statement which returns all values except those containing purely numeric values or blank spaces only.

ex if I have values

  1. ABCD
  2. abcd
  3. 1234
  4. AB12
  5. "?" (blank space)
  6. 12AB
  7. ab@!
  8. @!12
  9. !@#$

it should give all rows except 3rd and 5th


Solution

  • where ISNUMERIC(col) != 1 and len(rtrim(col)) >= 1
    

    full example:

    create table #t1 (col varchar(50))
    
    insert into #t1 (col) values('ABCD')
    insert into #t1 (col) values('abcd')
    insert into #t1 (col) values('1234')
    insert into #t1 (col) values('AB12')
    insert into #t1 (col) values(' ')
    insert into #t1 (col) values('12AB')
    insert into #t1 (col) values('ab@!')
    insert into #t1 (col) values('@!12')
    insert into #t1 (col) values('!@#$')
    
    select col    
    from #t1    
    where isnumeric(col) != 1 and len(rtrim(col)) >= 1
    
    drop table #t1