removing characters from field in MS Access database table

后端 未结 4 998
不知归路
不知归路 2021-01-18 21:27

Using MS Access 2010. I have a field in a table that contains windows path names surrounded by quotes, like this

\"C:\\My Documents\\Photos\\img1.jpg\"
\"C:\         


        
相关标签:
4条回答
  • 2021-01-18 21:44

    If you will be doing this from within an Access session, using Access 2000 or later, you can use the Replace() function in an update query to remove the quotes. Remove would mean replace them with an empty string.

    UPDATE YourTable
    SET path_field = Replace(path_field, '"', '');
    

    If any of those path strings could include quotes within them (yuck!), consider the Mid() function ... ask it to start at the 2nd character (skipping the lead quote), and return the number of characters equivalent to Len(path_field) - 2

    UPDATE YourTable
    SET path_field = Mid(path_field, 2, Len(path_field) - 2);
    

    Either way, you may want to include a WHERE clause to ignore rows without path_field values.

    WHERE Len(path_field) > 0
    

    And if you must do this again when new data is added, use a different WHERE clause to ensure you UPDATE only those rows whose path_field values start and end with quotes.

    WHERE path_field Like '"*"'
    

    That was using the * wild card for Access' default ANSI 89 mode. If you will do this from ADO (ANSI 92 mode), use the % wild card.

    WHERE path_field Like '"%"'
    

    ... or use ALike and the % wild card with either mode.

    WHERE path_field ALike '"%"'
    
    0 讨论(0)
  • 2021-01-18 21:47

    To make this a permanent change, you might run an update query that looked something like this:

    UPDATE [Your Table]
    SET [Your Table].[Your Field] = Replace([Your Table].[Your Field],"""","")
    

    This will get rid of all quotes, even if they aren't at the beginning or end. Post back if that's not exactly what you want.

    0 讨论(0)
  • 2021-01-18 21:47

    Assuming your column name is MyColumn and table name is MyTable, you can use this sql to update your data to get rid of quotes.

    UPDATE MyTable
    SET MyColumn = REPLACE(MyColumn,'"','')
    
    0 讨论(0)
  • 2021-01-18 21:59

    The solution with REPLACE already mentioned by others works, but removes ALL quotes, even if they are in the middle of the string.

    If you only want to remove quotes at the beginning or at the end, but leave quotes in the middle of the string as they are, you can do it with the following two queries:

    Remove first character if it's a quote:

    update YourTable
    set YourField = right(YourField, len(YourField) - 1)
    where left(YourField, 1) = '"'
    

    Remove last character if it's a quote:

    update YourTable
    set YourTable = left(YourField, len(YourField) - 1)
    where right(YourField, 1) = '"'
    
    0 讨论(0)
提交回复
热议问题