Case insensitive LIKE clause in Pervasive SQL - pervasive-sql

I'm attempting to write a query in Pervasive SQL which matches on a "LIKE" clause, but without case sensitivity.
As an example, I want the following query to match both "john", "John", and "JOHN". Currently, this is case sensitive.
SELECT name FROM table WHERE name LIKE ?
In T-SQL, I would just wrap UPPER around both parts of the WHERE clause, like this:
SELECT name FROM table WHERE UPPER(name) LIKE UPPER(?)
However, placing any functions to the right of the WHERE clause fails with a syntax error.
How can I achieve a case-insensitive search?

The only way I can think of is to change the case of the value that's coming in before you create the SQL Statement. Something like this C#ish code:
string value = "world";
sql = "SELECT name FROM table WHERE name LIKE = '" + value.ToUpper();
Or, even better use Parameters and set your value before you set the parameter.
You're right, having a function on the right side the LIKE will cause a Syntax Error. You might want to report it as a bug to Actian.

Related

Asp Classic & Firbird Sql without quotation marks

I have a script in ASP Classic that uses a Firebird database. I would like to execute a query without "quotation marks"
Now I must write this:
SQL = "SELECT ""TArticoli"".""IDArticolo"",""TArticoli"".""Desc"" FROM ""TArticoli"";"
I would write this:
SQL = "SELECT TArticles.IDArticle, TArticles.Desc FROM TArticles;"
The first one is accepted the second not, how can I do this?
You can't. DESC is a reserved word in Firebird, so to be able to use it as a column name (or any other object name for that matter), you will need to enclose it in quotes.
A second problem is that you are currently using
SELECT "TArticoli"."IDArticolo","TArticoli"."Desc" FROM "TArticoli"
And this means both your table name and the column names are case sensitive, and in that case, quoting those object names is mandatory. Unquoted object names are case insensitive, but are mapped to object names in upper case. This means that select * from TArticoli will select from a table called TARTICOLI, while select * from "TArticoli", selects from a table called TArticoli.
So unless you are going to rename or recreate all your tables or columns, you will not be able to get rid of quotes. The only thing you can do to reduce the number of quotes, is by not prefixing the columns with the table names (in the query shown it isn't necessary), or otherwise use a case insensitive alias for the table, eg
SELECT "IDArticolo", "Desc" FROM "TArticoli"
or
SELECT a."IDArticolo", a."Desc" FROM "TArticoli" AS a

Oracle SQL Query to find the existence of characters in string

I am using oracle database and trying to find out the query which should return the result when there is a special character(',`,(,)) exist on the string.
I am trying something like this,
select username from users where username like (',`,~,(,));
I tried to achieve the same using the below query,
select username from users where (username like '%`%' OR username like '%~%');
It doesn't consider the second condition and returns the value to the first condition only.
Is there any function/methods using which this result can be fetched?
You can use regular expressions and check all special characters with one condition:
SELECT username
FROM users
WHERE regexp_instr(username,'[''`\(\)]') > 0
Old school style without regexp
where length(translate(username, '_''`~()', '_')) <> length(username)

SQL WHERE is anything

I'm working on a database query via a search bar and would like it to sometimes yield all results (depending on what is inputted)
I know that for SELECT you can use * in order to select all columns. Is there similar SQL syntax: i.e. WHERE name IS * to essentially always be true?
Edit to clarify:
The nature of the clause is that a variable is used to set the name (I'm actually not able to change the clause, that was made clear). i.e. WHERE name IS [[inputName]] (inputName is the decided by the search bar)
WHERE ISNULL(name, '') = ISNULL(name, '')
(assuming that 'name' is of a string type)
Just make the column reference itself. However, if this is the only goal of your query, why are you against omitting the WHERE clause?
If you want to return all results in a SQL statement, you can simply omit the WHERE clause:
SELECT <* or field names> FROM <table>;
You should use WHERE only when you want to filter your data on a certain field. In your case you just don't want to filter at all.
Actually you don't need WHERE clause at all in this situation. But if you insist then you should write your predicate so it always returns true. This can be done many ways:
Any predicate like:
WHERE 1=1
With column:
WHERE name = name OR name is null
With LIKE:
WHERE name LIKE '%' OR name is null
With passed parameter:
WHERE name = #name OR #name is null
You can think of more of course. But I think you need the last one. Pass NULL from app layer if you want all rows.

SQL query using CONTAINS doesn't work on specify keyword

I am writing a simple query in SQL Server:
SELECT *
FROM Table1
WHERE CONTAINS(Column1, 'MY')
but it doesn't return any results. While using like it returns results.
Is there any specific reason why the keyword 'MY' doesn't work?
Update:
If I use other keywords, it works, only the specific 'MY' seems to be that I cannot used. My column is already set into fulltext index. Also for performance purposes I prefer to use CONTAINS.
you could do something like
select * from Table1 where Column1 like '%MY%'
When you use linq you can use the statements like you use in C#. It will be translated to SQL, a language which database can execute. So, try something like this:
string sql = "SELECT * FROM Table1 WHERE Column1 like #Column1";
And in your command, try to add parameters to replace the #Column1
command.Parameters.AddWithValue("#Column1", "%MY%");
The % char, represents anything. So, if you want to do a command like StartWith like we do from string, just use MY%.

Why won't this SQL statement work?

I have the following SQL-statement:
SELECT DISTINCT name FROM log WHERE NOT name = '' AND name LIKE '%.EDIT%';
It works fine on Postgres (returns all different names from log, which aren't empty and contain the string '.EDIT'). But on Oracle this statement doesn't work. Any idea why?
SELECT DISTINCT name FROM log WHERE NOT name = '' AND name LIKE '%.EDIT%';
1) Oracle treats '' as NULL, which means the comparison "NOT name = ''" is never true or false; use "IS NOT NULL" instead. But...
2) The second condition "name LIKE '%.EDIT%' will not match an empty string anyway, making the first condition redundant.
So re-write as:
SELECT DISTINCT name FROM log WHERE name LIKE '%.EDIT%';
The empty string in Oracle is equivalent to NULL, causing the comparison to fail.
Change that part of the query to NAME IS NOT NULL
You can rewrite that query without the "NOT NAME=''" clause.
SELECT DISTINCT name
FROM log
WHERE name LIKE '%.EDIT%';
Does that work for you?
If not, in what way does it not work? Does it cause an error? Are the wrong results returned?
Please expand your question with this info :-)