Unequal operators: != vs. <>, whats the difference? - operators

Whats the difference between != and <> when applied on numbers?
Example:
a != b
a <> b

There is exactly no difference at all in functionality. Both are same. Actually <> is the elder brother of !=
<> was being used from BASIC language
!= started from c

Related

Switching fields in WHERE clause SQL 2005 [duplicate]

I am creating a SQL query in which I need a conditional where clause.
It should be something like this:
SELECT
DateAppr,
TimeAppr,
TAT,
LaserLTR,
Permit,
LtrPrinter,
JobName,
JobNumber,
JobDesc,
ActQty,
(ActQty-LtrPrinted) AS L,
(ActQty-QtyInserted) AS M,
((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM
[test].[dbo].[MM]
WHERE
DateDropped = 0
--This is where i need the conditional clause
AND CASE
WHEN #JobsOnHold = 1 THEN DateAppr >= 0
ELSE DateAppr != 0
END
The above query is not working. Is this not the correct syntax or is there another way to do this that I don't know?
I don't want to use dynamic SQL, so is there any other way or do I have to use a workaround like using if else and using the same query with different where clauses?
Try this
SELECT
DateAppr,
TimeAppr,
TAT,
LaserLTR,
Permit,
LtrPrinter,
JobName,
JobNumber,
JobDesc,
ActQty,
(ActQty-LtrPrinted) AS L,
(ActQty-QtyInserted) AS M,
((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM
[test].[dbo].[MM]
WHERE
DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) != 1 AND DateAppr != 0)
)
You can read more about conditional WHERE here.
Try this one -
WHERE DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) != 1 AND DateAppr != 0)
)
To answer the underlying question of how to use a CASE expression in the WHERE clause:
First remember that the value of a CASE expression has to have a normal data type value, not a boolean value. It has to be a varchar, or an int, or something. It's the same reason you can't say SELECT Name, 76 = Age FROM [...] and expect to get 'Frank', FALSE in the result set.
Additionally, all expressions in a WHERE clause need to have a boolean value. They can't have a value of a varchar or an int. You can't say WHERE Name; or WHERE 'Frank';. You have to use a comparison operator to make it a boolean expression, so WHERE Name = 'Frank';
That means that the CASE expression must be on one side of a boolean expression. You have to compare the CASE expression to something. It can't stand by itself!
Here:
WHERE
DateDropped = 0
AND CASE
WHEN #JobsOnHold = 1 AND DateAppr >= 0 THEN 'True'
WHEN DateAppr != 0 THEN 'True'
ELSE 'False'
END = 'True'
Notice how in the end the CASE expression on the left will turn the boolean expression into either 'True' = 'True' or 'False' = 'True'.
Note that there's nothing special about 'False' and 'True'. You can use 0 and 1 if you'd rather, too.
You can typically rewrite the CASE expression into boolean expressions we're more familiar with, and that's generally better for performance. However, sometimes is easier or more maintainable to use an existing expression than it is to convert the logic.
The problem with your query is that in CASE expressions, the THEN and ELSE parts have to have an expression that evaluates to a number or a varchar or any other datatype but not to a boolean value.
You just need to use boolean logic (or rather the ternary logic that SQL uses) and rewrite it:
WHERE
DateDropped = 0
AND ( #JobsOnHold = 1 AND DateAppr >= 0
OR (#JobsOnHold <> 1 OR #JobsOnHold IS NULL) AND DateAppr <> 0
)
Often when you use conditional WHERE clauses you end upp with a vastly inefficient query, which is noticeable for large datasets where indexes are used. A great way to optimize the query for different values of your parameter is to make a different execution plan for each value of the parameter. You can achieve this using OPTION (RECOMPILE).
In this example it would probably not make much difference, but say the condition should only be used in one of two cases, then you could notice a big impact.
In this example:
WHERE
DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) <> 1 AND DateAppr <> 0)
)
OPTION (RECOMPILE)
Source Parameter Sniffing, Embedding, and the RECOMPILE Options
This seemed easier to think about where either of two parameters could be passed into a stored procedure. It seems to work:
SELECT *
FROM x
WHERE CONDITION1
AND ((#pol IS NOT NULL AND x.PolicyNo = #pol) OR (#st IS NOT NULL AND x.State = #st))
AND OTHERCONDITIONS

Why does my SQL query != "Some Value" not return any rows where NULL?

I'm querying an Oracle db for my MVC project and I found something in my SQL that I was curious about. Why does this Query not return rows where the VALIDATED_BY Column is NULL?
SELECT *
FROM DM_SSA_DEV.REQUESTS
WHERE BATCH_ID = 1399
AND VALIDATED_BY <> 'AUTOUSER'
When I commented out the last line I got my expected 481 results:
SELECT *
FROM DM_SSA_DEV.REQUESTS
WHERE BATCH_ID = 1399
--AND VALIDATED_BY <> 'AUTOUSER'
Finally I found I could circumvent this issue via explicitly handling the NULLs. Was this the right thing to do or was there a better way, and why?
SELECT *
FROM DM_SSA_DEV.REQUESTS
WHERE BATCH_ID = 1399
AND (VALIDATED_BY <> 'AUTOUSER' OR VALIDATED_BY IS NULL)
P.S. I'm also really using Linq in my project so this is the code I used. Most efficient?
db.REQUESTS.Where(r => r.BATCH_ID == batchId && (r.VALIDATED_BY != "AUTOUSER" || r.VALIDATED_BY == NULL)).ToList<REQUEST>();
NULL is never equal (=) to any other value, even another NULL (it is a bit like floating-point NaN). This also includes not equal expressions involving NULL.
The intent can be expressed as x = y OR (y IS NULL and x IS NULL), when NULL values are to be considered equal.
The special case of not-equal or NULL-and-not-equal is x <> y OR x IS NULL, assuming y is never NULL.
Another option is to coalesce, COALESCE(x, '*') = COALESCE(y, '*'), depending on how much the optimizer is trusted and the equality of the coalesced value to NULL. There may also be other vendor extensions.
LINQ/EF will translate x == null (C#, null must be a constant expression) to x IS NULL (SQL).

Comparing boolean subexpressions for equality in SQL (Microsoft SQL Server 2012)

I'm trying to use the following logical expression as part of a stored procedure for Microsoft SQL Server 2012, where #filter1 and #filter2 are of type bit (i.e. boolean).
((#filter1 <> 0) = (T.ID = '123')) and
((#filter2 <> 0) or (T.FK is null))
I get a syntax error at the first =. Evidently results of boolean terms can be combined using AND and OR but cannot be checked for equality with other such terms.
Is this indeed the case and if so, how would one normall reformulate such a condition for this SQL environment? Do I have to compare something like case when TERM then 1 else 0 end?
If memory serves me right you can't evaluate the assignment operation as a boolean value (in the way you can in C for instance) so I think you would have to do this:
((#filter1) = (case when T.ID = '123' then 1 end)) and
((#filter2 <> 0) or (T.FK is null))
This should work.
(((#filter1 <> 0) AND (T.ID = '123')) OR ((#Filter1 = 0) AND (t.ID <> '123'))) and
((#filter2 <> 0) or (T.FK is null))

Conditional WHERE clause in SQL Server

I am creating a SQL query in which I need a conditional where clause.
It should be something like this:
SELECT
DateAppr,
TimeAppr,
TAT,
LaserLTR,
Permit,
LtrPrinter,
JobName,
JobNumber,
JobDesc,
ActQty,
(ActQty-LtrPrinted) AS L,
(ActQty-QtyInserted) AS M,
((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM
[test].[dbo].[MM]
WHERE
DateDropped = 0
--This is where i need the conditional clause
AND CASE
WHEN #JobsOnHold = 1 THEN DateAppr >= 0
ELSE DateAppr != 0
END
The above query is not working. Is this not the correct syntax or is there another way to do this that I don't know?
I don't want to use dynamic SQL, so is there any other way or do I have to use a workaround like using if else and using the same query with different where clauses?
Try this
SELECT
DateAppr,
TimeAppr,
TAT,
LaserLTR,
Permit,
LtrPrinter,
JobName,
JobNumber,
JobDesc,
ActQty,
(ActQty-LtrPrinted) AS L,
(ActQty-QtyInserted) AS M,
((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM
[test].[dbo].[MM]
WHERE
DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) != 1 AND DateAppr != 0)
)
You can read more about conditional WHERE here.
Try this one -
WHERE DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) != 1 AND DateAppr != 0)
)
To answer the underlying question of how to use a CASE expression in the WHERE clause:
First remember that the value of a CASE expression has to have a normal data type value, not a boolean value. It has to be a varchar, or an int, or something. It's the same reason you can't say SELECT Name, 76 = Age FROM [...] and expect to get 'Frank', FALSE in the result set.
Additionally, all expressions in a WHERE clause need to have a boolean value. They can't have a value of a varchar or an int. You can't say WHERE Name; or WHERE 'Frank';. You have to use a comparison operator to make it a boolean expression, so WHERE Name = 'Frank';
That means that the CASE expression must be on one side of a boolean expression. You have to compare the CASE expression to something. It can't stand by itself!
Here:
WHERE
DateDropped = 0
AND CASE
WHEN #JobsOnHold = 1 AND DateAppr >= 0 THEN 'True'
WHEN DateAppr != 0 THEN 'True'
ELSE 'False'
END = 'True'
Notice how in the end the CASE expression on the left will turn the boolean expression into either 'True' = 'True' or 'False' = 'True'.
Note that there's nothing special about 'False' and 'True'. You can use 0 and 1 if you'd rather, too.
You can typically rewrite the CASE expression into boolean expressions we're more familiar with, and that's generally better for performance. However, sometimes is easier or more maintainable to use an existing expression than it is to convert the logic.
The problem with your query is that in CASE expressions, the THEN and ELSE parts have to have an expression that evaluates to a number or a varchar or any other datatype but not to a boolean value.
You just need to use boolean logic (or rather the ternary logic that SQL uses) and rewrite it:
WHERE
DateDropped = 0
AND ( #JobsOnHold = 1 AND DateAppr >= 0
OR (#JobsOnHold <> 1 OR #JobsOnHold IS NULL) AND DateAppr <> 0
)
Often when you use conditional WHERE clauses you end upp with a vastly inefficient query, which is noticeable for large datasets where indexes are used. A great way to optimize the query for different values of your parameter is to make a different execution plan for each value of the parameter. You can achieve this using OPTION (RECOMPILE).
In this example it would probably not make much difference, but say the condition should only be used in one of two cases, then you could notice a big impact.
In this example:
WHERE
DateDropped = 0
AND (
(ISNULL(#JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(#JobsOnHold, 0) <> 1 AND DateAppr <> 0)
)
OPTION (RECOMPILE)
Source Parameter Sniffing, Embedding, and the RECOMPILE Options
This seemed easier to think about where either of two parameters could be passed into a stored procedure. It seems to work:
SELECT *
FROM x
WHERE CONDITION1
AND ((#pol IS NOT NULL AND x.PolicyNo = #pol) OR (#st IS NOT NULL AND x.State = #st))
AND OTHERCONDITIONS

sql operator equivalent to !=

I want to select all the fields where a certain column is not a value I specify.
I tried this but it didn't work.
SELECT *
FROM table
WHERE columnname != value
My mistake.
It was another error so I thought it was wrong with != operator cause it was the first time I use it. Sorry guys!
SELECT *
FROM table
WHERE columnname <> value
For MySQL:
!= or <> are correct.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You should consider NULL columns also. You can do WHERE columnname IS NOT NULL also.
In SQL, I believe inequality is
<>
though many implementations also allow
!=
Either <> or !=
From: MySQL Manual (version 5.0)
<>, !=
Not equal:
mysql> SELECT '.01' <> '0.01';
-> 1 mysql> SELECT .01 <> '0.01';
-> 0 mysql> SELECT 'zapp' <> 'zappp';
-> 1
You need to post the query you are using, because != works fine for me in MySQL 4.1
As others mentioned, <> is equivalent. The != is ANSI standard (99 I believe).
WHERE NOT columnname = value
NOT IN is one flavor and here's a tsql negate example