php doctrine createQueryBuilder special character - doctrine-orm

Database store the value "&You" but the query below doesn't return result
$buildQuery = $this->createQueryBuilder('r')
->where('r.TitreSearch LIKE :term')
->setParameter('term', "%&You%")
;
On the other hand if I search "You" I have a result
why doctrine not understand character "&" ?

The problem is not the ampersand character but the query itself.
If you want LIKE to match a pattern within the column value, you must enclose the search term with percentage signs (i.e. %term%). Without the percentage signs, LIKE acts just the same as the = operator; it looks for an exact match.
Try the following:
$term = "&You";
$buildQuery = $this->createQueryBuilder('r')
->where('r.TitreSearch LIKE :term')
->setParameter('term', "%$term%");

Related

Wildcards as values in like operator return all data from the particular table. like '%%%%'

Generally we are doing a search operation by using like operator in sql.
For example,
If user doing search text ( input string ) as "Kannan" means It's should work operation like
Select * from users where name like <cfqueryparam value="%#url.search#%" cfsqltype="cf_sql_varchar">
The above cfm query should be like ( Select * from users where name like '%Kannan%' )
One of my end user search text ( input string ) is %%% means So the query like
Select * from users where name like '%%%%%'
It's return all the records from the user table. I don't have any name contains % But the search string (%%%) return all the records.
I've directly run the select * from users where name like '%%%%%' in my sql it's also return all records from the table.
I've go through the docs they mentioned as like '%%%' (or) like '%%%%' ( or ) if the % only in between start & end % (wildcards ) symbol without any other string mean it's return all the records from the particular table.
How I can handle this problem ? If the user search %%% then should not return any records until the name column having % value
Thanks is advance !
Change this:
Select *
from users
where name like <cfqueryparam value="%#url.search#%" cfsqltype="cf_sql_varchar">
to this:
<cfset searchString = "%" & replace(url.search, "%", "[%]", "all") & "%">
<cfquery name = "something">
Select *
from users
where name like <cfqueryparam value="#searchString#" cfsqltype="cf_sql_varchar">
</cfquery>
Escape the literal % character:
Select * from users where name like <cfqueryparam value="%#Replace(url.search,"%","\%","all")#%" cfsqltype="cf_sql_varchar">
You may try like this for search scenario. Hope, it may help to you.
In a LIKE pattern, the '%' matches any number of characters, including zero.
So, multiple '%' in a row do exactly the same thing. They match any number of characters, including zero characters.
The other wildcard, '_' behaves differently. It matches exactly one character -- not zero not two. So '__' matches exactly two. And '__%' matches a string with two or more characters.
EDIT:
If you want to search for the value explicitly, don't use like.
where instr(name, #uservalue)
Only use like if you want % and _ to be treated as wildcards. And, if you are thinking of allowing that, I would recommend regular expressions instead -- because they are more powerful and commonly used across many different systems (databases and otherwise).
At a total guess, but seems like you'll need to replace the characters in your parameter first, and then search:
SET #YourParameter = '%' + REPLACE(REPLACE(REPLACE(#YourParameter,'[','[[]'),'_','[_]'),'%','[%]') + '%';
SELECT {Your Columns}
FROM YourTable
WHERE YourColumn LIKE #YourParameter;
Mostly the search text shouldn't be only the % symbol. But the search text contains with some other alpha numeric value. for example user can using the search text 100%. So, the question is valid one. Here I would suggest you to use the Escape clause.
The given following SQL statement, the ESCAPE clause used to escape the character ! to negate the meaning of ‘%’ to find the string ‘100% Free’ in the column offer of the product table.
SELECT *
FROM products
WHERE offer LIKE '%100!% Free%' ESCAPE '!';
You may try with that in your search scenario. Hope, it may help to you.
Source from Using SQL LIKE with the ESCAPE clause here https://www.sqlshack.com/overview-of-the-sql-like-operator/

SQL pattern matching using regular expression

Can we use Regex i.e, Regular Expression in SQL Server? I'm using SQL-2012 and 2014 and there is an requirement to match and return input from my stored procedure.
I can't use LIKE in this situation since like only returns matching words, Using Regex I can match whole bunch of characters like Space, Hyphen, Numbers.
Here is my SP
--Suppose XYZ P is my Search Condition
Declare #Condition varchar(50) = 'XYZ P'
CREATE PROCEDURE [dbo].[usp_MATCHNAME]
#Condition varchar(25)
as
Begin
select * from tblPerson
where UPPER(Name) like UPPER(#Condition) + '%'
-- It should return both XYZ P and xyzp
End
Here my SP is going to return all matching condition where Name=XYZ P, but how to retrieve other Column having Name as [XYZP, XYZ-P]
and if search condition have any Alphanumeric value like
--Suppose XYZ 1 is my Search Condition
Declare #Condition varchar(50) = 'XYZ 1'
Then my search result should also return nonspace value like [XYZ1, xyz1, Xyz -1].
I don't want to use Substring by finding space and splitting them based on space and then matching.
Note: My input condition i.e., #Condition can have both Space or Space less, Hyphen(-) value when executing Stored Procedure.
Use REPLACE command.
It will replace the single space into %, so it will return your expected results:
SELECT *
FROM tblPerson
WHERE UPPER(Name) LIKE REPLACE(UPPER(#Condition), ' ', '%') + '%'

Regex to replace parameter names by values

I have a string that has a form:
UPDATE "TABLE"."ITEMS" SET ITM_DESCR=:sqldevvalue WHERE ROWID = :sqldevgridrowid AND ORA_ROWSCN = :sqldevgridrowscn
and its binding values as:
#1(11):Test Record #2(18):AAAG9IAAFAAAC0eAAB #3(7):7746161
How can I construct a regular expression to replace the parameter names (starting with :) with their corresponding values and create a combined string that has the form:
UPDATE "TABLE"."ITEMS" SET ITM_DESCR=Test Record WHERE ROWID = AAAG9IAAFAAAC0eAAB AND ORA_ROWSCN = 7746161
Here's a very simple, naive regex:
:(\w+)
Replace the match with the value, $1 contains the parameter name.
Here's a less naive attempt:
'(?:''|[^'])*'|:(\w+)
If $1 is set, replace the match with the value ($1 contains the paramter name), else do not replace. This version will let you handle situations like WHERE column = 'some text :not_a_param more text'
And... the not naive approach is to use prepared statements and SQL parameters with whatever SQL client you're using. This is the best option as it negates any risk of SQL injections if you do something wrong, and lets the DBMS cache the execution plan for your request.

query for name not like other name in neo4j cypher

I'm trying to find nodes where node1's name is NOT contained in node2's name and vice versa.
I've tried this and variants of it. getting regex errors, not sure how to include the other node's literal name, and also how to do NOT includes.
START node1=node(*)
MATCH node1-[r]-node2
WHERE node1.name !~ '.*{node2.name}.*' and node2.name !~ '.*{node1.name}.*'
RETURN node1.name, node2.name limit 10;
I'll try to answer how to get your query to work, but this type of query looks a bit irregular for Neo4j. If you find yourself writing many queries like these you may want to rethink your model or choice of database.
Your '.*{node2.name}.*' contains node2.name as a string literal, not as a property reference. To resolve the property and use it's value in a regular expression you can use string concatenation, something like '.*' + node2.name + '.*'. If node2.name='Darby' then the regexp string will be '.*Darby.*'.
Regexp is signified with =~, if you want to use ! to check for property existence you can do node.property! =~ regexp. To exclude the results of your double regexp condition, do WHERE NOT ( condition1 OR condition2 ).
Since the operator order between string concatenation and regexp comparison is not obvious, it's probably best to put the string concatenation in parenthesis too, or you may end up concatenating the result of a regexp on the first string part with the rest of the string parts, i.e (node.name =~ '.*') + node2.name + '.*', which would be a type error.
Assuming you use Neo4j 1.9 the whole query could look like (for 2.0+ you can just drop the !)
START node1=node(*)
MATCH node1-[r]-node2
WHERE NOT (
node1.name! =~ ('.*' + node2.name + '.*') OR
node2.name! =~ ('.*' + node1.name + '.*')
) RETURN node1.name, node2.name LIMIT 10
(It's an expensive type of query and it probably returns redundant results, since each node pair (A,B) that fit your conditions will be returned both as (A,B) and as (B,A). Try declaring direction on the relationship, it should exclude redundant results and improve performance.)
We can use this operator "<>"
eg : match(node:Application) where node.name <> "None" return node
This query is to return all the application node whose name is not "None".

DB2: find field value where first character is a lower case letter

I am trying to pick out a value in a field where the first character is a lower case letter. This is difficult since DB2 does not permit regular expressions. My current attempt is:
select * from mytable
where field1 like lcase('_%')
where I was hoping the underscore followed by percent wildcard would find any character in the first position, and then wrap the lcase() around that to ensure it is lower case. the result is that any and every value gets selected, so the lcase() is not performing what I want it to do, and in hindsight is used to cast to lowercase.
With that in mind, how to I ensure that the result of
('_%')
is lowercase only?
Thanks very much
i would use something like:
... where substr(field1,1,1) <> upper(substr(field1,1,1))
solution with 'a'...'z' will not work with characters different from latin characterset (e.g. cyrilic etc)
Why not
where field1 >= 'a' and field1 < '{'
This will even make use of an appropriate index, if any.
Be warned, however, that this won't work when your DB instance does lexicongraphic ordering. I am not sure if the latter is a DB attribute or a session attribute, however.
Another, more general way (especially when considering non ASCII letters) would be to check if the length of the field is > 0 and the lowercased substring consisting of the first character equals the substring consisting of the first character while the uppercased first character does not equal the first character. (Look up the functions in the DB2 reference, I have mine not ready at the moment.)
DB2 DOES allow Regular expressions with xQuery. For example:
with cteGender(VALUE) as
(
values
('M'),('F'),('U'),('S'),(' M'),('f')
),
cteResult(VALUE,RESULT_BOOLEAN) as
(
select '"' || VALUE || ‘"',
xmlquery('fn:matches($VALUE,''^[MFU]{1}$'')') from cteGender
)
select VALUE, RESULT_BOOLEAN,
xmlcast(RESULT_BOOLEAN as integer) RESULT_INTEGER from cteResult;
I took this example from: http://www.idug.org/p/bl/et/blogid=278&blogaid=187 That article explain very well how to use xQuery.
DB2 does not have SQL functions for Regular Expressions, but with xQuery you can do that. But if you really want SQL functions for RegEx, please visit this site: https://www.ibm.com/developerworks/jp/data/library/db2/j_d-regularexpression/ (In Japanese, but the code can be understood)
For more information about RegEx in DB2 please visit: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrregexp.html