How to escape bind variable (from Markdown) in Query? - sql

I created a markdown that will render a dropdown with values like these:
\server01\mis-home$\chq-blah\Scripts\Foo B Files.abc
\server02\mis-home$\chq-blah\Scripts\Bar G Files.abc
C:\Workspace\My_App\Resources\Export_Process.abc
When one of these values selected, subsequent query fails.
Markdown
Script = {!`MY AWESOME SELECT STATEMENT`}
Query
SELECT *
FROM myDB
WHERE scriptName =: Script
I suspect that's because of illegal characters such as space, backslash or underscore or dot. How do I tell Slamdata to escape those?

In order to reference a variable in SQL2, you must use the syntax :var_name, with no space between : and var_name.
If you change your query to the following:
SELECT * FROM myDB WHERE scriptName = :Script
Then it should function correctly.
SQL2 is more thoroughly documented on the Quasar Analytics website.

Related

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.

How to escape square bracket when using LIKE? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server LIKE containing bracket characters
I am having a problem with pattern matching.I have created two objects say,with codes
1)[blah1]
2)[blah2] respectively
in the search tab,suppose if i give "[blah" as the pattern,its returning all the strings
i.e., [blah1] and [blah2]
The query is
select *
from table1
where code like N'%[blah%'
I guess the problem is with the condition and special characters. Please do revert if you have as solution. Is there any solution where we can escape the character"[". I tried to change the condition as N'%[[blah%'.But even then its returning all the objects that is in the table.
When you don't close the square bracket, the result is not specified.
However, the story is different when you close the bracket, i.e.
select *
from table1
where code like N'%[blah%]%'
In this case, it becomes a match for (any) + any of ('b','l','a','h','%') + (any). For SQL Server, you can escape characters using the ESCAPE clause.
select * from table1 where code like N'%\[blah%\]%' escape '\'
SQL Fiddle with examples
You can escape a literal bracket character this way:
select *
from table1
where code like N'%[[]blah%'
Source: LIKE (Transact-SQL), under the section "Using Wildcard Characters As Literals."
I guess this is Microsoft's way of being consistent, since they use brackets to delimit table and column identifiers too. But the use of brackets is not standard SQL. For that matter, bracket as a metacharacter in LIKE patterns is not standard SQL either, so it's not necessary to escape it at all in other brands of database.
As per My understanding, the symbol '[', there is no effect in query. like if you query with symbol and without symbol it shows same result.
Either you can skip the unwanted character at UI Level.
select * from table1 where code like '%[blah%'
select * from table1 where code like '%blah%'
Both shows same result.

Regular expression to match a string in sql

How to write a regular expression to match a string if at least 3 characters from the start are matching?
Here is how my SQL query looks right now -
SELECT * FROM tableName WHERE columnName REGEXP "^[a-zA-Z]{3}someString";
You cannot use CONCAT or alike with REGEX, it will fail. Easiest way to do it, is:
$query = 'SELECT * FROM Test WHERE colb REGEXP "^'.substr($mystring,0,3).'"');
Another is:
SELECT * FROM Test WHERE LEFT(colb, 3) LIKE "{$mystring}%"
Please use jQuery and jqSQL plugin. Note that symbol $ must be escaped in SQL query with this plugin.

MySQL query problem with 'like' and confusion

I need to use a string query to make a DB search for a C# program that interacts with MySQL server. What I want to find is a name that is 'like' one of my other variables (nameVar)
I have the following query in a C# program
string q = "SELECT *
FROM TABLE
WHERE name is like %?nameVar%";
As soon as execute the query in my program I get a syntax error telling me that syntax near
'like' is incorrect. As soon as I remove the "%" sign, it works fine.
I am confused, is mandatory to remove the % sign while building a query string?
Your parameter is replacing the ?nameVar part, including quotes. If the param is "TEST", your query gets presented as
string q = "SELECT *
FROM TABLE
WHERE name is like %'TEST'%";
As you can see, the % signs are out of place. either include them from the C# program into namevar, or change the query to
string q = "SELECT *
FROM TABLE
WHERE name is like '%' + ?nameVar + '%'";
you need to quote the query:
string q = "SELECT * from table where name is like '%?nameVar%'";
Strings in SQL need to be enclosed in single quotes:
string q = "SELECT *
FROM TABLE
WHERE name LIKE '%?nameVar%' ";
Also, there's no IS operator when using LIKE.
I think the correct syntax is:
SELECT * FROM table WHERE fields LIKE '%phrase%'
I think you have to leave out 'is'.
MySQL Pattern Matching

File Name in the WHERE clause of Mysql returns nothing

I have the this SELECT query that returns nothing from the specified table yet the path to the file is stored in that table;
SELECT * from tableName WHERE imageFile = "C:\Documents and Settings\Albert Bayita\Desktop\MovieImages\TheLordOfTheRingsTheFellowship.jpg";
Any advice is appreciated.
The backslash character is the escape character in strings in MySQL. To put a backslash in a string literal in a query you have to escape it using double backslashes. Also a string in SQL uses apostrophes as delimiter, not quotation marks.
SELECT * from tableName WHERE imageFile = 'C:\\Documents and Settings\\Albert Bayita\\Desktop\\MovieImages\\TheLordOfTheRingsTheFellowship.jpg';
The best option is of course to use a parameterised query instead.
Your table tableName probably has no imageFile row with theat exact path.
Parhaps the path is stored differently (perhaps the \ is quoted to \)?
How do you know that is does exist exactly as you have entered in the query?
why you store the whole path in the database
you should figure out something to store just the image name
or if you want to keep on storing the whole path
try
to double the backslash \
SELECT * from tableName WHERE imageFile = "C:\\Documents and Settings\\Albert Bayita\\Desktop\\MovieImages\\TheLordOfTheRingsTheFellowship.jpg";