replacing a strange character

  • I have a special character in my table that I want to replace any where it is (while extracting the data). I do not even know how to replicate that character here to show it to you all. It's like a slanted " over the letters (like over A, B, etc) and it's generally there for Spanish names. I want to take it out.

    How can I do this? Thanks!

  • It's this character. I was able to copy it.

    Ñ

     

    Thanks!

  • SELECT REPLACE('ÑiÑo', 'Ñ', 'n')

  • But how can I do it for all characters? I need to search for any letter (Ato Z) that has that special character and then take it out.

  • Replace can only search one string at the time... you'll have to use multiple replaces :

    replace(replace(replace(YourCol, 'é', 'e'), 'è', 'e'), 'ê', 'e')

  • On a more basic note.  Why do you want to alter the data exactly?

  • I have to extract data from this table and load it into SAP which gives me an error when it finds this character. I could override it in SAP but that would effect all users, so I don't want to do that. Thanks.

  • Can I ask you something? How did you type the special character? What did you use on the key board? I know this sounds silly!

    Thanks!

  • Copy paste .

  • this might help: each of the characters can be used in a replace or something; SELECT char(169) returns ©

    for example.

    DECHEXCHAR
    3321!
    3422"
    3523#
    3624$
    3725%
    3826&
    3927'
    4028(
    4129)
    422A*
    432B+
    442C,
    452D-
    462E.
    472F/
    48300
    49311
    50322
    51333
    52344
    53355
    54366
    55377
    56388
    57399
    583A:
    593B;
    603C<
    613D=
    623E>
    633F?
    6440@
    6541A
    6642B
    6743C
    6844D
    6945E
    7046F
    7147G
    7248H
    7349I
    744AJ
    754BK
    764CL
    774DM
    784EN
    794FO
    8050P
    8151Q
    8252R
    8353S
    8454T
    8555U
    8656V
    8757W
    8858X
    8959Y
    905AZ
    915B[
    925C\
    935D]
    945E^
    955F_
    9660`
    9761a
    9862b
    9963c
    10064d
    10165e
    10266f
    10367g
    10468h
    10569i
    1066Aj
    1076Bk
    1086Cl
    1096Dm
    1106En
    1116Fo
    11270p
    11371q
    11472r
    11573s
    11674t
    11775u
    11876v
    11977w
    12078x
    12179y
    1227Az
    1237B{
    1247C|
    1257D}
    1267E~
    14591
    14692
    161A1¡
    162A2¢
    163A3£
    164A4¤
    165A5¥
    166A6¦
    167A7§
    168A8¨
    169A9©
    170AAª
    171AB«
    172AC¬
    173AD­
    174AE®
    175AF¯
    176B0°
    177B1±
    178B2²
    179B3³
    180B4´
    181B5µ
    182B6
    183B7·
    184B8¸
    185B9¹
    186BAº
    187BB»
    188BC¼
    189BD½
    190BE¾
    191BF¿
    192C0À
    193C1Á
    194C2Â
    195C3Ã
    196C4Ä
    197C5Å
    198C6Æ
    199C7Ç
    200C8È
    201C9É
    202CAÊ
    203CBË
    204CCÌ
    205CDÍ
    206CEÎ
    207CFÏ
    208D0Ð
    209D1Ñ
    210D2Ò
    211D3Ó
    212D4Ô
    213D5Õ
    214D6Ö
    215D7×
    216D8Ø
    217D9Ù
    218DAÚ
    219DBÛ
    220DCÜ
    221DDÝ
    222DEÞ
    223DFß
    224E0à
    225E1á
    226E2â
    227E3ã
    228E4ä
    229E5å
    230E6æ
    231E7ç
    232E8è
    233E9é
    234EAê
    235EBë
    236ECì
    237EDí
    238EEî
    239EFï
    240F0ð
    241F1ñ
    242F2ò
    243F3ó
    244F4ô
    245F5õ
    246F6ö
    247F7÷
    248F8ø
    249F9ù
    250FAú
    251FBû
    252FCü
    253FDý
    254FEþ
    255FFÿ

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Here's a quick, brute force way. It involves creating a table to map characters with ascii codes above 127 to other characters. You could create CharMap as a temp table or table variable if you like. In fact, it could be included in the UDF. I'd rather have a permanent table if I'm going to reference it many times.  At first, I created a set-based UDF, but that required an additional UDF (fn_Split, which can be found on this site) to convert the string into a table.

    Anyway, next, create the UDF FixChars().

    Finally, see the sample execution at the end.

    ---------------------------------------------------------------------------

    DROP TABLE CharMap

    GO

    CREATE TABLE CharMap

    (

      prevAscii tinyint

    , prevChar varchar(1)

    , newChar varchar(2)

    )

    GO

    INSERT CharMap (prevAscii, prevChar, newChar)

      SELECT 138, 'Š', 'S'

      UNION ALL

      SELECT 142, 'Ž', 'Z'

      UNION ALL

      SELECT 158, 'ž', 'z'

      UNION ALL

      SELECT 159, 'Ÿ', 'Y'

      UNION ALL

      SELECT 192, 'À', 'A'

      UNION ALL

      SELECT 193, 'Á', 'A'

      UNION ALL

      SELECT 194, 'Â', 'A'

      UNION ALL

      SELECT 195, 'Ã', 'A'

      UNION ALL

      SELECT 196, 'Ä', 'A'

      UNION ALL

      SELECT 197, 'Å', 'A'

      UNION ALL

      SELECT 198, 'Æ', 'AE'

      UNION ALL

      SELECT 199, 'Ç', 'C'

      UNION ALL

      SELECT 200, 'È', 'E'

      UNION ALL

      SELECT 201, 'É', 'E'

      UNION ALL

      SELECT 202, 'Ê', 'E'

      UNION ALL

      SELECT 203, 'Ë', 'E'

      UNION ALL

      SELECT 204, 'Ì', 'I'

      UNION ALL

      SELECT 205, 'Í', 'I'

      UNION ALL

      SELECT 206, 'Î', 'I'

      UNION ALL

      SELECT 207, 'Ï', 'I'

      UNION ALL

      SELECT 208, 'Ð', 'D'

      UNION ALL

      SELECT 209, 'Ñ', 'N'

      UNION ALL

      SELECT 210, 'Ò', 'O'

      UNION ALL

      SELECT 211, 'Ó', 'O'

      UNION ALL

      SELECT 212, 'Ô', 'O'

      UNION ALL

      SELECT 213, 'Õ', 'O'

      UNION ALL

      SELECT 214, 'Ö', 'O'

      UNION ALL

      SELECT 216, 'Ø', 'O'

      UNION ALL

      SELECT 217, 'Ù', 'U'

      UNION ALL

      SELECT 218, 'Ú', 'U'

      UNION ALL

      SELECT 219, 'Û', 'U'

      UNION ALL

      SELECT 220, 'Ü', 'U'

      UNION ALL

      SELECT 221, 'Ý', 'Y'

      UNION ALL

      SELECT 222, 'Þ', ''  -- ?

      UNION ALL

      SELECT 223, 'ß', 'B'

      UNION ALL

      SELECT 224, 'à', 'a'

      UNION ALL

      SELECT 225, 'á', 'a'

      UNION ALL

      SELECT 226, 'â', 'a'

      UNION ALL

      SELECT 227, 'ã', 'a'

      UNION ALL

      SELECT 228, 'ä', 'a'

      UNION ALL

      SELECT 229, 'å', 'a'

      UNION ALL

      SELECT 230, 'æ', 'ae'

      UNION ALL

      SELECT 231, 'ç', 'c'

      UNION ALL

      SELECT 232, 'è', 'e'

      UNION ALL

      SELECT 233, 'é', 'e'

      UNION ALL

      SELECT 234, 'ê', 'e'

      UNION ALL

      SELECT 235, 'ë', 'e'

      UNION ALL

      SELECT 236, 'ì', 'i'

      UNION ALL

      SELECT 237, 'í', 'i'

      UNION ALL

      SELECT 238, 'î', 'i'

      UNION ALL

      SELECT 239, 'ï', 'i'

      UNION ALL

      SELECT 240, 'ð', 'o'

      UNION ALL

      SELECT 241, 'ñ', 'n'

      UNION ALL

      SELECT 242, 'ò', 'o'

      UNION ALL

      SELECT 243, 'ó', 'o'

      UNION ALL

      SELECT 244, 'ô', 'o'

      UNION ALL

      SELECT 245, 'õ', 'o'

      UNION ALL

      SELECT 246, 'ö', 'o'

      UNION ALL

      SELECT 247, '÷', ''  -- ?

      UNION ALL

      SELECT 248, 'ø', ''  -- ?

      UNION ALL

      SELECT 249, 'ù', 'u'

      UNION ALL

      SELECT 250, 'ú', 'u'

      UNION ALL

      SELECT 251, 'û', 'u'

      UNION ALL

      SELECT 252, 'ü', 'u'

      UNION ALL

      SELECT 253, 'ý', 'y'

      UNION ALL

      SELECT 254, 'þ', ''  -- ?

    GO

    --DROP FUNCTION dbo.FixChars

    GO

    CREATE FUNCTION dbo.FixChars

    (

      @s-2 varchar(8000)

    )

    RETURNS varchar(8000)

    AS

    BEGIN

      DECLARE @newString varchar(8000)

            , @C varchar(1)

            , @newChar varchar(2)

            , @pos int

           

      SET @pos = 1

      WHILE @pos <= Len(@s)

      BEGIN

        SET @C = Substring(@s, @pos, 1)

        IF Ascii(@c) > 127

        BEGIN

          SELECT @newChar = Coalesce(m.newChar, @C)

            FROM CharMap m

           WHERE Ascii(@c) = m.prevAscii

          SET @s-2 = Stuff(@s, @pos, 1, @newChar)

          SET @pos = @pos + Len(@newChar)

        END -- IF

        ELSE

          SET @pos = @pos + 1

         

      END --WHILE

      RETURN @s-2

    END

    GO

    -------------------------------------------------------------

    -- Test data

    -------------------------------------------------------------

    DECLARE @test-2 TABLE (data varchar(100))

    INSERT @test-2

      SELECT 'This is á TÈŠT striÑg'

      UNION ALL

      SELECT 'This is ánother TÈŠT striÑg'

      UNION ALL

      SELECT 'This is still á TÈŠT ŠtriÑg'

    SELECT data FROM @test-2

    SELECT dbo.FixChars(data) FROM @test-2

    UPDATE @test-2 SET data = dbo.FixChars(data)

    SELECT data FROM @test-2

  • Thanks, that helps! Also, to type the character, we can use ALT plue 0 plus the code

  • We have that issue with information pasted into our web pages and then that information needs to be copied, (and altered) into our SPs. 

    We created a table and function to handle this.  Basically you can use the function on the string passed in - you may need to increase the length from 2000 to something larger...

    Here is the table with some data - I would highly suggest using the ASCII characterset and including Descriptions! 

    IF EXISTS( SELECT * FROM dbo.sysobjects WHERE id = object_id( N'dbo.SpecialCharacters') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)

    DROP TABLE dbo.SpecialCharacters

    GO

    CREATE TABLE dbo.SpecialCharacters(

     RowID integer IDENTITY (1, 1) NOT NULL,

     SearchForCharacter varchar(1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

     ReplacementCharacter varchar(1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

     Explanation varchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

    ) ON [PRIMARY]

    GO

    CREATE UNIQUE CLUSTERED INDEX [cuidx_SearchForCharacter] ON dbo.SpecialCharacters( SearchForCharacter) ON [PRIMARY]

    GO

    CREATE INDEX [idx_RowID] ON dbo.SpecialCharacters( RowID) ON [PRIMARY]

    GO

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(8), CHAR(32), 'Replace a Backspace with a Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(9), CHAR(32), 'Replace a Horizontal Tab with a Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(10), CHAR(32), 'Replace a Line Feed with an Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(11), CHAR(32), 'Replace a Vertical Tab with a Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(12), CHAR(32), 'Replace a Form Feed with an Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(13), CHAR(32), 'Replace a Carriage Return with an Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(27), CHAR(32), 'Replace an Escape Character with an Space'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(96), CHAR(39), 'Replaces CHAR(96) [ ` ] with a Single Quote'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(145), CHAR(39), 'Replaces CHAR(145) [ ‘ ] with a Single Quote'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(146), CHAR(39), 'Replaces CHAR(146) [ ’ ] with a Single Quote'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(180), CHAR(44), 'Replaces an Odd Comma CHAR(180) [ ´ ] with the Standard Comma'

    INSERT INTO SpecialCharacters( SearchForCharacter, ReplacementCharacter, Explanation)

    SELECT CHAR(184), CHAR(44), 'Replaces an Odd Comma CHAR(184) [ ¸ ] with the Standard Comma'

    Here is the funtion:

    IF EXISTS( SELECT * FROM dbo.sysobjects WHERE id = object_id( N'dbo.ReplaceSpecialCharacter') AND xtype IN( N'FN', N'IF', N'TF'))

    DROP FUNCTION dbo.ReplaceSpecialCharacter

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS OFF

    GO

    CREATE FUNCTION  dbo.ReplaceSpecialCharacter( @ValueToAlter AS varchar(2000)) RETURNS varchar(2000)

    AS

    BEGIN

     SELECT @ValueToAlter = REPLACE( @ValueToAlter, SearchForCharacter, ReplacementCharacter) FROM SpecialCharacters

     RETURN ( @ValueToAlter )

    END

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

    I wasn't born stupid - I had to study.

  • I'd like to personally applaud Farrell for getting off his training wheels and using a set based approach VS his old usual bulldozzer approach (even if he found this solution here) !

     

    Congrats

  • That's quite the left-handed compliment...  (Where did I find that here?) 

    I wasn't born stupid - I had to study.

Viewing 15 posts - 1 through 15 (of 16 total)

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