Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Trim Leading and Trailing Quote from MySQL Row?

Tags:

I have a MySQL table that I have imported from a CSV file. In this process, a bunch of the entries have quote marks leading and trailing the entry of several data rows. For example the the table 'example_table' I have a row called 'title.' Some of these titles are written as:

"title1" "title2" "title3" 

and some are written without the quote marks:

title4 title5 title6 

I have tried a variety of SQL calls to trim the row but I keep getting errors. Here is my sql call:

SELECT * FROM `example_table` TRIM(LEADING '"' FROM "title") 

This is the error from MySQL when I run the call:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

How do I go about getting rid of all the trailing and leading quotation marks from the row?

like image 337
codacopia Avatar asked Jul 16 '11 16:07

codacopia


People also ask

How do I remove quotation marks in MySQL?

Try this: str_replace('"', "", $string); str_replace("'", "", $string); Otherwise, go for some regex, this will work for html quotes for example: preg_replace("/<!

How do I remove a single quote from a MySQL query?

You can easily escape single quotes, double quotes, apostrophe, backticks and other special characters by adding a backslash (\) before that character.

How do I trim a word in MySQL?

TRIM() Function in MySQL BOTH | LEADING | TRAILING : LEADING, TRAILING, or BOTH option to explicitly instruct the TRIM() function to remove leading, trailing, or both leading and trailing unwanted characters from a string . By default, the TRIM() function uses the BOTH option.

How do I remove special characters from a MySQL query?

MySQL a-z in Telugu You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.

How to remove leading and trailing spaces from a string in MySQL?

Let’s take some examples of using the MySQL TRIM () function. The following statement uses the TRIM () function to remove both leading and trailing spaces from a string.

How do I use TRIM () function in MySQL?

MySQL provides a very useful string function named TRIM () to help you clean up the data. The following illustrates the syntax of the TRIM () function. The TRIM function provides a number of options. You can use the LEADING, TRAILING, or BOTH option to explicitly instruct the TRIM () function to remove leading, trailing, ...

How do I remove leading and trailing characters in SQL?

Code language: SQL (Structured Query Language) (sql) The TRIM function provides a number of options. You can use the LEADING, TRAILING, or BOTH option to explicitly instruct the TRIM () function to remove leading, trailing, or both leading and trailing unwanted characters from a string. By default, the TRIM () function uses the BOTH option.

How to remove trailing characters in MySQL data cleansing?

One of the most important tasks in data cleansing is to remove the unwanted leading and trailing characters. MySQL provides a very useful string function named TRIM () to help you clean up the data.


1 Answers

Try:

UPDATE `example_table`     SET `title` = TRIM(BOTH '"' FROM `title`) 

This query will updated your example_table to remove leading and trailing double quotes from the value of the title column.

If you don't want to update the table, but want to fetch the rows with double quotes removed, then use @Sam Dufel's answer.

like image 95
Shef Avatar answered Oct 10 '22 10:10

Shef