Oracle to SQL Server conversion - sql

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?

Related

Add keyword before and after cast() in oracle

I would like to add a keyword before and after each field value in Oracle.
For example if I am getting 123 as my ID, I would like to make it
Test123Test
Here is my query:
SELECT
CAST("ID" as varchar(10))
FROM
TABLENAME;
I have tried add + "Test" but it is giving me error.
Use || instead of + to concatenate strings in Oracle.
SELECT 'test' || CAST(ID as varchar(10)) || 'test'
FROM TABLENAME
Note that I removed the " around ID too, since you most likely won't need them and they can cause problems when it unintended strictly matches column names.
I have tried add + "Test" but it is giving me error.
Perhaps, + is used as concatenation in SQL Server. In Oracle, you could use the CONCAT function or the || operator.
The concat function is restricted to only two strings. You can have a look the concat function in the documentation http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions026.htm.
Let's see an example using the operator -
SELECT 'test' || to_char(id) || 'test' new_id
FROM TABLENAME

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)

WHERE clause with OR operator not giving intended result

I want to write a stored procedure for a search function. I have a database of movies and I should be able to search a movie by Movie.Name, Movie.Producer, Movie.Director and appearing Cast.Name.
CREATE PROCEDURE RetrieveSearchResults
#tokenParam VarChar
AS
Select *
from
(Movie
join
Cast on Movie.MovieID = Cast.MovieID)
join
Actor on Actor.ActorID = Cast.ActorID
where
(Movie.Name like '%' + #tokenParam + '%')
or (Movie.Producer like '%' + #tokenParam + '%')
or (Movie.Director like '%' + #tokenParam + '%')
or (Actor.Name like '%' + #tokenParam + '%')
Upon execution
RetrieveSearchResults 'Almighty'
I get almost all tuples with no similar literal as 'Almighty'
Am I missing something?
Try declaring the varchar limit. VARCHAR(100) or something like that. It may be making assumptions on the length and cutting it down to a VARCHAR(1) or similar. The problem with declaring VARCHAR without a length is the system will assign for you and it may not be what you expect.

Oracle LIKE-wildcard in inner SELECT query

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).

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