MySQL: how to use comma, single quote, and double quote as columns? - mysql

I'm trying to pivot a table so I can output the data as a CSV. I need to do something like this:
SELECT .... t1.`column_one`, t1.`column_two`, ...
Problem is that some of the columns are expected to contain commas, single quotes, and double quotes.
Is there a way to make something like this work:
SELECT .... t1.`foo's, "bar"`, ...
The above doesn't work. Suggestions?

I've tested and can confirm that the following definitely works:
SELECT `t1`.`foo's, "bar"` FROM `t1`;
The only thing I could suggest is to place the table name between ` (backtick) characters.

According to the MySQL documentation you should be fine as long as the column name is ASCII, doesn't contain an ASCII NUL (0x00) and is under 64 characters long in total. What do you mean by "doesn't work"? Does it give an error message?

Related

Search for text within square brackets and speech marks SQL

I am wanting to search for text within brackets and speech marks. The values in the column look like this: ["flour","butter","eggs"]
I have tried:
SELECT * FROM 'posts' WHERE ingredients = '''butter'''
Th, however,er does not work.
How can I write a query to return the rows only containing the desired words? All help is much appreciated.
Ps. I know the data should not have been stored like this and it is not in the correct normal form. I just need a way to search for this for now.
Well, if this is JSON, you should store it as JSON, not a string.
That said, you can use LIKE:
SELECT p.*
FROM posts p
WHERE ingredients LIKE '%"butter"%';
Some comments about quotes. You seem very confused by quotes in your query.
You have 'posts' as a table name in single quotes. This will actually return an error. The only quoting allowed here are escape characters (backticks). However, they are not needed.
Your data has double quotes around the values. However, you have doubled up single quotes in your comparison string. Single quotes and double quotes are different things.

Escaping a forward slash in an SQL name? It can be "escaped", but SQL believes it to be multiple columns

The last person in my job has flooded column names with special characters such as (?,!, and /), as well as used many reserved keywords for column names (more often than not, timestamp or user is used).
Normally, I step around this by using double quotes or brackets to escape the SQL object. A subset of the full list of columns are below:
DriverID,
Department,
Odometer,
MerchantState,
MerchantCity,
gallons/Units,
timestamp,
tax
Inside my query, I wrap the two columns in question (gallons/units and timestamp) inside double quotes. Timestamp because it's a reserved keyword, and Gallons/units, because without the quotes, SQL reads the query, stops at the slash, and tells me "Gallons" is not a column inside the table.
If I do wrap double quotes around the column name, SQL returns a different error: "Operand should contain 1 column(s)".
I've tried every variant (only capturing the slash in quotes, quoting both, using brackets, mixing brackets and quotes, etc. but with to no avail).
Is there anything I can do to fix this query short of renaming the column name and changing the associated code in the program that pulls from it? (the really tedious task I'm trying to avoid).
In SQL Server, identifiers can be delimited using square brackets, e.g.
SELECT [gallons/units] ...
In MySQL, identifiers can be delimited using backticks, e.g.
SELECT `gallons/units` ...
(NOTE: If MySQL SQL_MODE includes ANSI_QUOTES, then double quotes are treated as delimiters for identifiers, similar to the way Oracle handles double quotes; absent that setting, double quotes are handled as delimiters for string literals. With ANSI_QUOTES included SQL_MODE, "gallons/units" will be interpreted as an identifier (column name). Without ANSI_QUOTES, MySQL will see it as a string literal, as if it were enclosed in single quotes.)
FOLLOWUP:
As far as an error "operand should contain only 1 column(s)", that's usually a problem with query semantics, not an issue with escaping identifiers.
A subquery in the SELECT list can return only a single expression, for example, this would throw an error:
Query: SELECT 'foo' , ( SELECT 'fee' AS fee, 'fi' AS fi )
Error Code: 1241
Operand should contain 1 column(s)
You can try backticks instead of double quotes
`gallons/units`
There are a couple of options. First, have you tried using %/ to escape the slash?
Example: "select * from 'gallons%/units';"
Second one I've found, which may or may not be helpful, is to provide an escape character definition, such as
http://blogs.msdn.com/b/zainala/archive/2008/08/17/using-and-escape-clause-in-sql-server-like-query.aspx
select * from MyTable where Description like '|[summary|]%' escape '|'
In your case
select * from 'gallons|/units' escape '|'
You indicate both mysql and sql-server in your tags, so I'm not sure which server support I should be looking for exactly.

Search of String value having special character in MySQL

I have one table emp in MySQL database having column as name. In that name column, the value is 'abc\xyz'. I want to search this value. I have tried using following query:
select * from emp where name like 'abc\xyz';
Also i have tried
select * from emp where name like 'abc\xyz' escape '\\';
But i did not found any output. Could you please help me in finding such strings? Such strings can have special character at any location.
Thanks in advance.
You may try like this:
select * from emp
where empname like '%abc\\\\xyz%'
SQL Fiddle Demo
From the docs:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
SELECT REPLACE(text,'\\','') FROM tbl
You can use REPLACE to remove some special chars :)

MySQL backslash apostrophe

I'm having a MySQL issue.
I'm trying to select all rows in a table that start with a backslash and an apostrophe:
SELECT * FROM table WHERE name like '\\\'%'
But this is not working. An example of what I'm trying to select: \'S-GRAVENDEEL
How do I do this?
Thanks
p.s.
Yes, this was the result of a faulty import, I know, but now I need to fix it :-)
You need more backslashes:
select * from table where name like '\\\\\'%'
You need one of them to get a single quote into the pattern. Then you need four more to get a single literal backslash down to the like. Or you could escape the single quote by doubling it:
select * from table where name like '\\\\''%'
So I've got a solution.
Basically what I want to do is fix the entries, so I'll show you what the replace looks like:
SELECT *, REPLACE(naam, '\\''', '''') naamnew FROM school_plaats WHERE naam like '%\\''%'
Apparently, I need to escape the apostrophe with an apostrophe and the backslash with a backslash.

How do apply SQL like on "\detail1\detail2\" (Escaping '\')?

I have a table T1 with a column details, data column has following sample data :
select details from T1;
\this is detail 1\this is detail2\
\this is detail 1\this is detail2\
:this is detail 1:this is detail2:
:this is detail 1:this is detail2:
\this is detail 1\this is detail2\
I want to fetch only those details which are separated by \.
For that I wrote the following query :
select details from T1 where details like '\%\%\';
But its not responding as expected, since its taking \ as escape corrector.
So, its showing error of incomplete single inverted(') !
So, how to do this ?
Try this, you need to escape backslashes twice in LIKE statement.
select details from T1 where details like '\\\\%\\\\%\\\\'
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
MySql has escape character \\ for backslash (same as in C-like languages):
http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
Try using '\\%\\%\\' - using a backslash to escape the backslash.