Oracle LIKE-wildcard in inner SELECT query - sql

In SQL Server T-SQL I used to use the scenario like this
SELECT .. FROM .. WHERE sometable.eng LIKE (SELECT tmpcolumn FROM tmptable WHERE tmpID = #counter) + '%';
How to pass LIKE (subquery) + '%' in Oracle correcly?
Does it actually work for Oracle 11g+ or not?
.. smth LIKE (SELECT .. FROM ..) + '%';
The underscore _ for fixed length doesn't fit my needs, so % only.

Oracle uses || for string concatenation, not +. So it should be:
smth LIKE (SELECT .. FROM ..) || '%'

This seems like such an odd formulation. Just as a note, I would write the query as:
SELECT ..
FROM ..
WHERE EXISTS (SELECT 1
FROM tmptable
WHERE tmpID = #Counter AND
sometable.eng LIKE tmpcolumn || '%'
);
Putting a subquery between the keyword LIKE and the wildcard makes the query harder to read (at least for me).

Related

LIKE clause applied to a DB Type in Stored Procedure

I have created the following Type:
CREATE TYPE dbo.expressions
AS TABLE
(
expression varchar(255)
);
GO
How can I, in a Stored Procedure, use the n types that I receive and do, with each one, an select with a like clause?
In terms of "programming" (and pseudo-code), would be something like this:
for each expression in expressions
rows+=select * from table where table.field LIKE '%' + expression[i] + '%'
I can always call the SP multiple times from my API but I was wondering if this is possible, and even faster, to do in SQL side.
You simply SELECT from / JOIN with an instance of the type. Assuming you have something like:
CREATE TYPE expressions AS TABLE (expression varchar(255));
CREATE PROCEDURE mysp(#expressions AS expressions) ...
You can use the variable like you would use a table:
SELECT *
FROM #expressions AS expressions
INNER JOIN yourtable ON yourtable.field LIKE '%' + expressions.expression + '%'
The above will allow you to use expression in select clause. Otherwise you can use the following:
SELECT *
FROM yourtable
WHERE EXISTS (
SELECT 1
FROM #expressions AS expressions
WHERE yourtable.field LIKE '%' + expressions.expression + '%'
)

Query filter delphi firedac Firebird database

I´m migrating a database from SQLITE to Firebird, but now my query doesn't work.
What am I doing wrong? Is there a reason?
frmDados.Clientes.Close();
frmDados.Clientes.SQL.Text :=
'SELECT * FROM CLIENTES ' +
'WHERE (nomecliente like :d1) '+
'order by nomecliente asc';
frmDados.Clientes.Params.ParamByName('d1').AsString := '%' + Edit1.text + '%';
frmDados.Clientes.OpenOrExecute();
Firebird dose not support case insensitive queries.
Query_Case_Insensitive.html
Consider these options
select * from "abc_table" where "Some_Field" = 'Abc'
select * from "abc_table" where "Some_Field" like 'Abc'
select * from "abc_table" where "Some_Field" containing 'Abc'
select * from "abc_table" where upper("Some_Field") = 'ABC'
Equals (=) and *like * both perform case sensitive matches
*containing * is case insensitive, but will also match 'abcd'
upper() works, but will not use an index and, therefore, will read every record in the table
Equals (=) is the fastest because it uses an index (if available)

If Else Statement in SQL Server stored procedure

In SQL Server, on .NET you can add together a SQL statement and therefor use either 'or' or 'and'.
I am stuck how to do this in a SQL Server stored procedure.
Question:
What is it called what you add together SQL statements? I have a feeling it starts with a 'c' concocations??
I believe I am close with the below code to having a 'variable' SQL?
Code:
BEGIN
SELECT *
FROM TblEmployee
WHERE
FirstName LIKE '%' + LTRIM(RTRIM((#sFirstName))) + '%'
or
Surname LIKE '%' + LTRIM(RTRIM((#sSurname ))) + '%'
and
IF (LTRIM(RTRIM((#sOfficeBase))) = 'xyz1234')
and OfficeBase = LTRIM(RTRIM((#sOfficeBase)))
ELSE
or
OfficeBase= LTRIM(RTRIM((#sOfficeBase)))
END
There are simular queries such as If else in stored procedure sql server
But I have a feeling I searching for the 'wrong' question. Thanks in advance
The where clause allows you to embed conditional logic - you don't need if/then/else, which is just as well, because afaik, that's not supported by any SQL dialect in a where clause.
BEGIN
SELECT *
FROM TblEmployee
WHERE
FirstName LIKE '%' + LTRIM(RTRIM((#sFirstName))) + '%'
or
Surname LIKE '%' + LTRIM(RTRIM((#sSurname ))) + '%'
and
(OfficeBase = 'xyz1234'
or
OfficeBase= LTRIM(RTRIM((#sOfficeBase)))
END
I am pretty sure you have some missing parenthesis in your original query. Not sure why you are adding LTRIM and RTRIM to your parameters. Taking a shot in the dark I suspect you could boil down your query to this.
SELECT * --This should be the column names actually needed, * is not good for production code
FROM TblEmployee
WHERE
(
FirstName LIKE '%' + #sFirstName + '%'
or
Surname LIKE '%' + #sSurname + '%'
)
and OfficeBase in ('xyz1234', #sOfficeBase)

Oracle to SQL Server conversion

What is the equivalent of SQL Server query form for the below Oracle query in a LIKE statement:
((UPPER(ADDRESS) like '%'|| UPPER(:VALUE1) || '%' ) OR (ADDRESS IS NULL AND :VALUE1 IS NULL))
I am stuck with the syntax '%'||------||'%'.
|| is Oracle's concatenation operator. SQL Server's equivilent is +.
((UPPER(ADDRESS) like '%' + UPPER(#VALUE1) + '%' )
OR (ADDRESS IS NULL AND #VALUE1 IS NULL))
Incidentally, || is the SQL standard, + is used by some databases, like SQL Server instead of following the standard.
((UPPER(ADDRESS) like '%' + UPPER(VALUE1) + '%' ) OR (ADDRESS IS NULL AND VALUE1 IS NULL))
Note that using upper is only necessary depending on the collation setting.
You can use + instead of || to concatenate the strings
((UPPER(ADDRESS) like '%'+ UPPER(VALUE1) + '%' ) OR (ADDRESS IS NULL AND VALUE1 IS NULL))
You also do not need to use upper in comparison for SQL Server. Comparisons in SQL Server are case insensitive.
Is this part of a where clause or something?

SQL Search Query With Null and ''

I have a query that I'm building for an application. The database is setup in SQL Server 2008. I want to use a query similar to below, however, I will be using this 'Where' clause for about 4 other columns using the same requirements. Is this the appropriate way to test for null or '' in a column that is VarChar(255) and does allow nulls.
Ideally, if the variable #UutSerialNumber is null or empty (''), I want all the results, but if it is not, I want to use the 'LIKE' clause. Is this the proper way of doing this and will it work? It seems to work until I start adding more columns to the Where clause.
Also, how would I handle a "text" datatype using the same type of query?
SELECT DeviceName, UutStatus
FROM MyTable
WHERE (UutSerialNumber LIKE '%' + #UutSerialNumber + '%' OR UutSerialNumber LIKE '%%' AND (#UutSerialNumber = '' OR #UutSerialNumber IS NULL)) AND ...
Help is appreciated. Thanks everyone!
It amy seem like duplication of SQL but the best way to do this is in terms of performace is using IF ... ELSE
IF ISNULL(#UutSerialNumber, '') = ''
BEGIN
SELECT DeviceName, UutStatus
FROM MyTable
-- MORE EXPRESSIONS
END
ELSE
BEGIN
SELECT DeviceName, UutStatus
FROM MyTable
WHERE (UutSerialNumber LIKE '%' + #UutSerialNumber + '%'
-- MORE EXPRESSIONS
END
It can be done within the WHERE clause if you are doing it on multiple columns and the query you posted wasn't far off it was just missing additional parenthesis along with having a redundant clause.
SELECT DeviceName, UutStatus
FROM MyTable
WHERE (ISNULL(#UutSerialNumber, '') = '' OR UutSerialNumber LIKE '%' + #UutSerialNumber + '%')
AND (ISNULL(#AnotherParameter, '') = '' OR AnotherColumn LIKE '%' + #AnotherParameter + '%')
Convert the text type to VARCHAR(MAX).
as a footnote, I personally would use the CHARINDEX rather than concatenating strings in the like:
WHERE (ISNULL(#UutSerialNumber, '') = '' OR CHARINDEX(#UutSerialNumber, UutSerialNumber) > 0)
This is nothing more than a footnote however as I have done no performance testing, I just think it is easier on the eye!
SELECT DeviceName, UutStatus
FROM MyTable
WHERE ((#UutSerialNumber = '') OR (#UutSerialNumber is null)) OR (UutSerialNumber like #UutSerialNumber)
add '%' to the last #UutSerialNumber if you think you need