Unix Technical Forum

Function to remove accent in string

This is a discussion on Function to remove accent in string within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, Does anyone have a function which replaces accent chars from a string with the non-accented equivalent? For example ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:54 AM
Darren Jensen
 
Posts: n/a
Default Function to remove accent in string

Hi,

Does anyone have a function which replaces accent chars from a string
with the non-accented equivalent? For example 'hôpital' should return
'hopital'.

Thank you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:54 AM
David Portas
 
Posts: n/a
Default Re: Function to remove accent in string

Is this so that you can compare differently accented strings? If so and if
you are using SQL2000 then there is no need actually to replace the accented
characters. Just use an accent-insensitive collation for your comparisons:

IF 'hôpital'='hopital' COLLATE Latin1_General_CI_AI
PRINT 'YES'

This avoids an expensive update. Better still, if you can change the column
collation to be accent-insensitive then you can create an index on the
column to help with the comparison.

Otherwise you could replace the accented chars like this:

UPDATE YourTable SET col =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(col COLLATE Latin1_General_CI_AI
,'a','a'),'b','b'),'c','c'),'d','d'),'e','e'),'f', 'f')
,'g','g'),'h','h'),'i','i'),'j','j'),'k','k'),'l', 'l')
,'m','m'),'n','n'),'o','o'),'p','p'),'q','q'),'r', 'r')
,'s','s'),'t','t'),'u','u'),'v','v'),'w','w'),'x', 'x')
,'y','y'),'z','z')

Again, this assumes you are using 2000. If correct case is important to you
then specify a case-sensitive collation in place of Latin1_General_CI_AI and
add nested REPLACE statements for all the upper-case letters too.

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:55 AM
Darren Jensen
 
Posts: n/a
Default Re: Function to remove accent in string

Thanks David.

I tried changeing the column to be accent-insensitive as you suggest
and indeed it does work when using a simple SQL statement with a WHERE
clause (WHERE 'hôpital'='hopital'). However, I am using freetext
search on this column and when I use CONTAINSTABLE there is no result
found for 'hopital' it only works for 'hôpital'. Do you have any ideas
how I can get this to work without using a replace function?

Darren.

"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message news:<pr2dnfJO_6QZCTzcRVn-tw@giganews.com>...
> Is this so that you can compare differently accented strings? If so and if
> you are using SQL2000 then there is no need actually to replace the accented
> characters. Just use an accent-insensitive collation for your comparisons:
>
> IF 'hôpital'='hopital' COLLATE Latin1_General_CI_AI
> PRINT 'YES'
>
> This avoids an expensive update. Better still, if you can change the column
> collation to be accent-insensitive then you can create an index on the
> column to help with the comparison.
>
> Otherwise you could replace the accented chars like this:
>
> UPDATE YourTable SET col =
> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
> REPLACE(REPLACE(col COLLATE Latin1_General_CI_AI
> ,'a','a'),'b','b'),'c','c'),'d','d'),'e','e'),'f', 'f')
> ,'g','g'),'h','h'),'i','i'),'j','j'),'k','k'),'l', 'l')
> ,'m','m'),'n','n'),'o','o'),'p','p'),'q','q'),'r', 'r')
> ,'s','s'),'t','t'),'u','u'),'v','v'),'w','w'),'x', 'x')
> ,'y','y'),'z','z')
>
> Again, this assumes you are using 2000. If correct case is important to you
> then specify a case-sensitive collation in place of Latin1_General_CI_AI and
> add nested REPLACE statements for all the upper-case letters too.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:56 AM
David Portas
 
Posts: n/a
Default Re: Function to remove accent in string

I'm not an expert with Full Text but see this thread:
http://www.google.com/groups?hl=en&l...G P12.phx.gbl

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:07 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com