THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

How to select only alphabetical values?

Last post 05-19-2008, 11:19 by Diana. 3 replies.
Sort Posts: Previous Next
  •  05-09-2008, 14:23 6736

    How to select only alphabetical values?

    Received this question in a private e-mail from Vishnu:


    I want a select query  which should select values containing only alphabets ie (a - z only),

    it should not include any numbers(0-9) or hyphen(-)


     Can you help me.

    Thanks.

    Vishnu

     
     

  •  05-09-2008, 15:08 6738 in reply to 6736

    Re: How to select only alphabetical values?

    besided number and hyphens are there any other special characters?  If not then this should work

     CREATE TABLE #foo (Value VARCHAR(20))
    INSERT INTO #foo
    SELECT '1' UNION ALL
    SELECT 'A' UNION ALL
    SELECT 'B' UNION ALL
    SELECT '2' UNION ALL
    SELECT '33.331' UNION ALL
    SELECT 'adada'UNION ALL
    SELECT '1d2' UNION ALL
    SELECT '17777.999'


    SELECT * FROM #foo
    WHERE Value  NOT LIKE '%[0-9]%'
    AND Value  NOT LIKE '%[-]%'

  •  05-09-2008, 17:00 6741 in reply to 6738

    Re: How to select only alphabetical values?

    Wouldn't this be more accurate?

    -- Case Sensitive
    SELECT * FROM #foo WHERE Value NOT LIKE '%[^a-zA-Z]%'

    -- Case Insensitive
    SELECT * FROM #foo WHERE Value NOT LIKE '%[^A-Z]%'

  •  05-19-2008, 11:19 6856 in reply to 6736

    Re: How to select only alphabetical values?

    You can use "PATINDEX", like here: http://sqltips.wordpress.com/category/tips/

    If you work with SQL 2005, a CLR function and "regex" will also "do the trick".

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement