Will Comparing a Value Against Itself Fail in SQL WHERE Clause - sql

Question, if someone mistyped a comparison in the AND part of a WHERE clause, so that they ended up comparing a value against itself, would that throw an error, or would it simply fail silently? I ask because I came across a WHERE clause that includes the following line:
WHERE ...
AND (note_details.id_number = note_details.id_number)
How would this be handled?

It would work. There is no error.
The logic:
where x = x
is equivalent to:
where x is not null

Related

SQL: Calling REPLACE() with a NULL replacement

I've recently run into this behaviour and whilst I can work around it, I'm quite curious as to why it occurs. There doesn't seem to be much documentation explaining why this happens.
Consider the following two snippets:
SELECT REPLACE('hello world', 'world', NULL)
SELECT REPLACE('hello world', 'foobar', NULL)
Both of these examples return NULL.
I don't really understand either, but with the first one I can see how if there is indeed some caveat I'm unaware of with NULLs that this will cause the outcome we see.
But with the second example I'm completely stumped. Even if there is some strange NULL replacing behaviour, why is my string being manipulated at all? The replacement string is not found in the target string.
Read this:
https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql
It states that:
Returns NULL if any one of the arguments is NULL.
This is most certainly a result of the messed up NULL handlich in SQL Server. Everything you perform with NULL will result in NULL as well. Try SELECT 1 + NULL or 'abc' + NULL. Anyways, since you are working with a string, you should use the empty string '' instead of NULL.

SQL What does IF evaluate to for NULL

Hi this is a pretty basic question, but I am not able to find a definitive answer to it.
What does IF evaluate to for NULL in SQL ? What will happen in following code if fieldXYZ were NULL
IF fieldXYZ=SomeValue
//do something
ELSE
//do something else
Update:
I have found one result which suggests that NULL can be considered as falsy
https://technet.microsoft.com/en-us/library/ms182587(v=sql.110).aspx
First of all, I think your if..else statement is incomplete as you are not defining any expression for your column fieldXYZ. Generally, when you write an if..else statement, you would want to write an expression that compares your column value with something. You can explicitly check for null values if you want to handle them. Here is a gist of that syntax:
--This handles null check explicitly
IF fieldXYZ IS NOT NULL AND fieldXYZ = 'somevalue'
//do something
ELSE
//do something else
Also, if the fieldXYZ is passed a NULL value in the above statement, then the first condition is not met, so the else condition applies immediately in that scenario.Hope this helps!
(I am reluctant to answer my own question, but in absence of any solution, reposting my comment as answer)
Apparently the right way is to consider NULL as falsy.

What is the difference between keeping column on left of = in sql

I am reading someone else sql and his code was like this
There is view called user_v with column path as Array
select * from user_v where 'USER_TYPE'=path[2]
can't i use
path[2] = 'USER_TYPE'
This is a precaution taken by some programmers in languages where assignment and comparison can be easily confused, such as C or PHP, where the following statement looks innocent:
if ( $foo = 1 )
but it is actually assigning 1 to $foo, and the if will always evaluate to true (in PHP, at least, where 1 is true). What was meant was if ( $foo == 1 ).
If you reverse the arguments, the error becomes obvious sooner:
if ( 1 = $foo ) # SYNTAX ERROR
if ( 1 == $foo ) # Desired behaviour
This is sometimes known as "yoda coding", and is in the coding standards of Wordpress, for example.
See also: Why put the constant before the variable in a comparison?
In SQL, there is less chance of such a muddle, since although = can mean either assignment or comparison, there are rarely situations where a typo would select the wrong meaning.
However, if the coding standards for every other language used by a project mandate it, it would make sense to reinforce the habit by also using it in SQL, since I can't think of a specific reason not to write it that way.
There is no difference at all.
It's psychology.
You would want to read someone else's code out laud and say:
Where my column equals 2.
When you read:
Where 2 equals my column
you have to stop for a while, return, explain it to yourself.
We maintain all of these rules that seem rubish at first glance just to make other people lives easier.

Netsuite CASE WHEN expression in custom search is returning "invalid expression"

I am new to writing CASE expressions in NetSuite. I have inserted the following expression in a formula field of a custom search.
CASE WHEN {item.custitem_custid}=05 OR {item.custitem_custid}=12 THEN
({item.custitem_margin}/2)
ELSE
({item.custitem_margin}/3)
END
and getting "invalid expression as a result. I am attempting to divide the margin field value by 2 if the WHEN case is true and divide by 3 if not true. Anybody have any idea as to what is wrong with my formula?
I think you need to add parentheses:
CASE WHEN ({item.custitem_custid}=05 OR {item.custitem_custid}=12) THEN
({item.custitem_margin}/2)
ELSE
({item.custitem_margin}/3)
END
I agree with egrubaugh360 that the syntax is fine and likely the joins are not working correctly. In your forumula, just double-check the "item." portion and see what you can do.
I also agree with the NVL() recommendation, but I can also see that your denominator is 2 and 3, respectively, therefore the NVL() function won't do anything to help. PLEASE DO USE NVL() every time your denominator is an unknown value, such as from a field on your record/transaction.

VB.net Strange Conditional Statement (IF)

Was wondering if someone could lend me their expertise. Pretty new to Vb.net and have come across this conditional statement in one of our products. Could someone please confirm the validity of the statement and explain what's going on here? I've tried numerous searches, but I cannot find anything related.
If (IsDBNull(dr("someID")), "0", dr("someID")) = someID.ToString() Then
I have changed the "id" value names as it's code from a commercial product, but the ID's used were all the same variable (ints).
Thanks for any input you can offer on this!
Joe
PS: The reason I can't check this at run time is because of how the product operates.
It is an inline If statement
If(condition,iftrue,iffalse) if condition is true evaluate and return iftrue else iffalse
The If operator in VB.NET 2008 acts as a ternary operator.[ REFERENCE]
Example:
Dim foo as String = If(bar = buz, cat, dog) 'Condition satisfied then it'll return cat else dog.
The statement is checking to see if the dr("SomeID") equals the value someID.ToString. The reason the If is required is because you need to check if the dr("someID") Is Null. If it is 0 is used instead which presumably should not be equal to someID.
It is the same as doing the following:
If Not IsDBNull(dr("someID")) Then
If dr("someID").ToString = someID.ToString Then
End If
End If
I would suggest that something like this would be more appropriate (checking integer values instead of comparing strings)
If(IsDBNull(dr("someID")), 0, CInt(dr("someID"))) = someID Then
I would also suggest Turning Option Strict On as the code you posted should not compile!