Search code examples
sqlsql-serverdiacriticsnon-ascii-characters

Check If the string contains accented characters in SQL?


I want to perform a task if the input string contain any accented characters else do another task in SQL. Is there any way to check this condition in SQL ?

Eg:

@myString1 = 'àéêöhello!'

IF(@myString1 contains any accented characters)
  Task1
ELSE
  Task2

Solution

  • SQL Fiddle: http://sqlfiddle.com/#!6/9eecb7d/1607

    declare @a nvarchar(32) = 'àéêöhello!'
    declare @b nvarchar(32) = 'aeeohello!'
    
    select case 
        when (cast(@a as varchar(32)) collate SQL_Latin1_General_Cp1251_CS_AS) = @a 
        then 0 
        else 1 
    end HasSpecialChars
    
    select case 
        when (cast(@b as varchar(32)) collate SQL_Latin1_General_Cp1251_CS_AS) = @b 
        then 0 
        else 1 
    end HasSpecialChars
    

    (based on solution here: How can I remove accents on a string?)