How to select records with a range of ASCII character codes

  • Hi,

    I would like to pull back a results set with records that contain non-alphanumeric characters in a certain column. Is this possible using the ASCII codes?

    Just thinking of a quicker way rather than using multiple LIKE statements.

    Thanks!

    Richard

  • Try PATINDEX Function.

  • Will this work?

    select * from YourTable where YourColumn like '%[^a-zA-Z0-9]%'

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • RyanRandall (8/22/2008)


    Will this work?

    select * from YourTable where YourColumn like '%[^a-zA-Z0-9]%'

    Thanks Ryan - That returns all alphanumeric values but I want the opposite. Tried the same as far as listing them within the square brackets but that didn't work.

    Currently looking at the PATINDEX function.

  • richard (8/22/2008)


    That returns all alphanumeric values but I want the opposite.

    Huh? Did you see the not symbol (^)? Here's an example...

    --Sample Data

    declare @t table (v varchar(100))

    insert @t

    select 'H[1'

    union all select 'y}1'

    union all select 'a1'

    union all select '1'

    union all select 'g'

    union all select ''

    union all select null

    --Query

    select * from @t where v like '%[^a-zA-Z0-9]%'

    /* Output

    v

    -----

    H[1

    y}1

    */

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Sorry, my bad, did not see the ^ symbol.

    Works great now - thanks!

  • No worries - glad to help 🙂

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Another alternative may not be the best one

    SELECT

    YOURCOLUMN,

    CASE WHEN PATINDEX('%[~,!,@,#,$,^,&,*,(,),_,+,=,-,|,\,:,;,",?,/,.,>,<,`]%',YOURCOLUMN) !=0

    THEN 'Non - Alphanumeric'

    ELSE 'Alphanumeric'

    END AS FLAG

    FROM YOURTABLE

    Thanks,

    Amit Khanna

  • How can I add apostrophe to this expression - char(39)?

    For example, I should select all alpha-numeric name. However, some names contains apostrophe, like O'Brien.

    select * from YourTable where YourColumn like '%[^a-zA-Z0-9'']%'

    Did not work

  • I should select all alpha-numeric name

    This makes it sound like you have a different requirement, since the original poster was asking for non-alphanumeric data. Maybe you want something like this?

    --Sample Data

    declare @t table (v varchar(100))

    insert @t

    select 'H[1'

    union all select 'y}1'

    union all select 'a1'

    union all select '1'

    union all select 'g'

    union all select ''

    union all select null

    union all select 'smith'

    union all select 'smith1'

    union all select 'smith$'

    union all select 'o''brien'

    --Query

    select * from @t where v not like '%[^a-zA-Z'']%'

    /* Output

    v

    -----

    g

    smith

    o'brien

    */

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Thanks Ryan

    😀

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply