Results 1 to 7 of 7

Thread: Replacing a character with the " character in a database field

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    476

    Replacing a character with the " character in a database field

    I had to do a quick import of information into a MySQL database and double quotes were in the original data. The program I am working with does not handle them properly, so I did a search and replace with another character for the import.

    Then I needed to go back into the file at the database level and replace the character after importing.

    So basically this text

    Long 2" straps

    would not import properly. So I just changed it.

    I was wondering what the appropriate SQL statement would be to do this at the SQL level. I did resolve it and fixed it up by using MS Access and linking to a table and using the search and replace feature. I just didn't have time to sort it out with SQL. However, writing proper syntax would have been faster.

    So, for future reference, I was wondering what the appropriate syntax is to do this? I tried the following (no conditions, I wanted to update any possible record - unless that is my problem - didn't think of it until just now)

    Code:
    Update myTable
    Set textfield = Replace (textfield, 'CharacterUsed', '"')
    I tried this and it didn't update anything. I am assuming I have to escape the " character but I can't find any examples and don't know how to do this.

    TIA, rasinc

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Replacing a character with the " character in a database field

    That's the correct syntax, did it give you a error?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    476

    Re: Replacing a character with the " character in a database field

    No errors, just no records affected.

  4. #4
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Replacing a character with the " character in a database field

    This might sound dumb, but did you use the right special character? I've done things like that before and was scratching my head wondering why its not working.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Replacing a character with the " character in a database field

    If you are looking to remove the " character in this text you posted

    Long 2" straps

    with a BLANK, for instance...

    Then wouldn't it be Replace(textfield,'"',' ')

    Or is mysql replace backwards compared to MSSql in the order of replace arguments.

    btw - sometimes the " is really a unicode `` or something odd like that - are you sure you are using the right "original character"...

    For instance - can you search for the " with a WHERE clause that you type as :

    Long 2" straps

    Or do you need to copy/paste that data to make the WHERE clause work?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    476

    Re: Replacing a character with the " character in a database field

    Basically I had the text

    Long 2" straps

    and converted it to the following to import it

    Long 2$$ straps

    and then wanted to change it back to

    Long 2" straps

    for the client to use it.

    I'll test it again tomorrow. I am not in the office where the full data set it right now.

    Thanks for your responses. I'll check it again. Maybe I just messed up the syntax but was in too much of a hurry to try to figure it out. So I just tried another method.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    476

    Re: Replacing a character with the " character in a database field

    Finally had a chance to get back to this again. Sorry for the delay. I spent some time redoing everything from the export from Excel to the import.

    I found that when exporting from Excel as a *.prn file, the extra ""'s do not get inserted when there is a " in the text. This will help me in the future.

    Anyway, back to the issue at hand, it turns out that I must have done something wrong with my syntax. I went back and copied my pseudocode from Post #1 and put in my real table, field name and character used and it updated without any problems.

    So my apologies to everyone for such a silly mistake but going back and reviewing everything made me learn something as well. Thanks to smendoza and szlamany for helping. The answers were in your posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width