SQLCMD LIKE query returns more records than expected - sqlcmd

I have a sqlcmd utility file that is returning far more records than expected. Here is the essential structure of the file:
sqlcmd -S %1 -d %2
"Set NOCOUNT ON;
SELECT
col1,
col2,
col3
FROM some_table
WHERE
(
col1 LIKE '%text1%' OR col1 LIKE '%text2%'
OR
col2 LIKE '%text1%' OR col2 LIKE '%text2%'
OR
col3 LIKE '%text1%' OR col3 LIKE '%text2%'
)"
-s " "
-o some_output_file.txt
The stuff in the -s line is actually a tab character to make the output file be tab-delimited. I'm getting my entire some_table returning in this query. It should only return just a few records out of it. I have tested the SQL in SQL Server, and it works fine. However, when I run this with sqlcmd (via Command line), it returns the entire some_table. I don't know why. I think it may have something to do with wildcard characters in sqlcmd, but I haven't been able to find any documentation.

I found out what my error was. Since I am doing this via a Command prompt (i.e., a batch file), I need to escape the percent signs, as they are interpreted as by Windows. So, for the LIKEs, I need to escape them by using %% instead of just %.

Related

When executing MySQL file from command line, how to make the result yielded with special characters escaped?

I intend to execute a SQL as following and get the result outputted to terminal:
echo "select * from tbl limit 10" | mysql -hxxx -uxxx -pxxx -Dxxx
there's one field's content having single-quote in it, which is a JSON. I'd like the result being "{\'a\': 1}", yet what it yields is "{'a': 1}".
OUTFILE could not be applied in current scenario. Could anyone help out? Thanks!

Handle star * entry in mysql query from shell script

I use the following construct in a shell script (using dash, but I think in that case there is no difference to bash):
for FIELD in `echo "select column_with_varchar_type from table" | mysql MYDATABASE`
do
echo "FIELD: $FIELD"
done
This works fine for normal strings, but it fails for an entry where star ("*") is in column_with_varchar_type, then dash expands this to use all files in the current working directory which is of course completely wrong.
Is there a way to prevent the shell expansion or replace the star with something else in the mysql-query?
Clarification: I do NOT want to select all columns "select *", but I want to select all entries of one column and one of these entries is a string that is a star. The input to the query does not contain any stars and is fine, the problem lies in the output of the query.
For example, if the table contains the strings "aaa", "bbb" and "*", a typical output may be:
FIELD: aaa
FIELD: bbb
FIELD: Makefile
FIELD: src
FIELD: some_other_file_that_happens_to_be_in_the_working_directory.txt
So I get the strings "aaa" and "bbb" (correct), but "*" is missing and is replaced with whatever garbage is in the working directory.
Updated
As discussed in comments, this can be an approach:
while read -r field
do
echo "FIELD: $field"
done < <(echo "select column_with_varchar_type from table" | mysql MYDATABASE)
Because the problem was with the data coming from the query, not from the query itself. Hence, it is necessary to handle the strings with * that are coming from such query and while -r solves it.
The problem is that * gets expanded by the shell when it is within double quotes.
You can use single quotes instead:
for FIELD in $(echo 'select * from table' | mysql MYDATABASE)
do
do_something
done
Note you could also use -e option:
mysql MYDATABASE -e 'select * from table'
try this:
for FIELD in `echo 'select \* from table' | mysql MYDATABASE`
do
do_something
done
The solution is to add quotes in the MySQL-query:
for FIELD in `mysql MYDATABASE -e 'select concat(concat("\"", column_with_varchar_type), "\"") from table'`
do
echo "FIELD: $FIELD"
done

How to remove multiple strings in a Mysql column

I would like to trim this string (these strings are stored in a single column)
[17332] Rohayati Bakri [20611] Nazrie Haslieanie [20612] Nur Izzati
into this (also stored in a single column)
Rohayati Bakri, Nazrie Haslieanie, Nur Izzati
using only Mysql query. The reference id -> [xxxxx] all have the same length.
Is it possible?
Unfortunately, MySQL doesn't support any kinds of regular expressions in REPLACE. A brute force method is:
Dump the table
Replace strings with sed
Import table
The commands are:
mysqldump -u login -p db your_table > dump.sql
sed 's/\[[0-9]\{5\}\]//g' dump.sql > newdump.sql
mysqlimport -u login -p db your_table < newdump.sql
Potentially this could affect some other data that matches the pattern, so use this on your own risk.
You can try trim function also
UPDATE table_name SET col_name = SUBSTRING(col_name, 7)
It will trim first 7 caracters.
At a very generic level you can try it
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%'
In your case you say these were escaped but since you don't specify how they were escaped, let's say they were escaped to GREATERTHAN
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')
WHERE articleItemLIKE '%GREATERTHAN%'
Since your query is actually going to be working inside the string, your WHERE clause doing it's pattern matching is likely to improve any performance - it is actually going to generate more work for the server. Unless you have another WHERE clause member that is going to make this query perform better, you can simply do an update like this:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')
Coming in from a completely different angle:
You can do this when you select the data (not when you do save it)
So instead of :
SELECT MyURLString From MyTable
You can do
SELECT REPLACE (MyURLString, 'GREATERTHAN', '<') as MyURLString From MyTable
This could be done with regular expressions.
SELECT REPLACE(column, '\[[0-9]*\]', '') FROM table;
If MySQL supported them. Unfortunately MySQL does not support regular expressions in REPLACE.
But you could do it with a UDF. Here is a post that might help you.

Escaping spaces in a select result

I have the following select statement, for which the result is sent to a command line parameter:
"SELECT show.file \
FROM show, schedule \
WHERE channel = 1 AND start_time <= UNIX_TIMESTAMP()"
However, if the result returned has spaces in it, it will cause the command to fail. How can I escape out any spaces in the result? Note that this select statement will only ever return one result.
SELECT REPLACE(show.file, " ", "\ ")
FROM show, schedule
WHERE channel = 1 AND start_time <= UNIX_TIMESTAMP()
Should do the trick. If you have another escape character replace \ with the respective one.
See also
http://dev.mysql.com/doc/refman/5.1/de/string-functions.html
If Markus' solution didn't work, try using the 'xargs' command.
xargs YOUR_COMMAND YOUR_QUERY_RESULT_AS_PARAMETER
depending on the command you're gonna use, it may or may not work.

MySQL: How to set Linesize and other environment parameters

I need to set linesize to some queries I perform which are quite important. Does anyone know how?
If you are looking for an equivalent output formatting in MySQL to Oracle's linesize & pagesize, I would suggest using raw output mode. If you need line wrapping at a certain length rather than the full unwrapped line, you can pass it through a command line utility such as Unix fmt:
echo "SELECT col1, col2 FROM tab1;" | mysql --raw --batch -uuser -p dbname | fmt --width=80