Search code examples
sqlsql-server-2008selectintnvarchar

How select `Only String` and `Only Int`


I have some nvachar data in a my database.

aaaa , bb1b, c+cc , 1234 , dser , 5896

I need to two select for Only String and Only Int. This is mean :

Result of first select be : aaaa , dser

Result of second select be : 1234 , 5896

How?


Solution

  • You can use LIKE to perform this comparison. By using a double-negative, you can perform the tests with a single expression each:

    declare @t table (val varchar(10) not null)
    insert into @t(val)
    select 'aaaa' union all
    select 'bb1b' union all
    select 'c+cc' union all
    select '1234' union all
    select 'dser' union all
    select '5896'
    
    select * from @t where val not like '%[^A-Z]%'
    select * from @t where val not like '%[^0-9]%'
    

    The first select says "give me all strings that don't contain a non-alphabetic character". Similarly, the second says "give me all strings that don't contain a non-digit"