IS NULL function not working - google-bigquery

I've tested IS NULL function provided by Google BigQuery, but it doesn't seems to give correct results.
SAMPLE DATA:
id age gender password
1, 11, NULL, NULL
1, 11, "NULL", "NULL"
1, 11, "null", "null"
1, , NULL, NULL
QUERY:
SELECT id, age, gender, password, id IS NULL, age IS NULL, gender IS NULL, password IS NULL
FROM privatedata.testnull
OUTPUT:
Row id age gender password f0_ f1_ f2_ f3_
1 1 11 NULL NULL false false false false
2 1 11 NULL NULL false false false false
3 1 11 null null false false false false
4 1 0 NULL NULL false false false false
But this SQL works:
SELECT NULL IS NULL, COUNT(*) FROM privatedata:testnull
So I'm not sure if IS NULL is working or not. Also I'm confused with how I would insert null data in string format and numeric format.

EDITED ANSWER:
For very old tables there was an issue where we did not preserve the between an empty field and a NULL one. This issue should by fixed for any table that was created after March, 2013.
OLD ANSWER:
I believe the issue is that for string fields, we're interpreting the NULL as the string "NULL". What if you just use , , for the string values?
The one that is surprising is that age in row 4 should be showing up as null. It looks like something is wrong with the IS_NULL function. I've filed a bug internally. The IS_EXPLICITLY_DEFINED() function, however, should return what you expect (e.g. IS_EXPLICITLY_DEFINED for age in row 4 would return fasle).

Related

Branches coverage in Mockk Kotlin

I am using mockk library in Kotlin. I am covering branch coverage. I am new to testing. Can someone tell me how to cover all branches? In the given below example, it has two objects one is id as string and the name which is hidden is list. Thanks
Can someone explain what are the 12 branches are for this?
I can only count 9, but I might be missing something:
!id.isNullOrEmpty()
Reason
!xxx.isNullOrEmpty()
Reason
TRUE
id is not null and not empty
TRUE
xxx is not null and not empty
TRUE
id is not null and not empty
FALSE
xxx is empty
TRUE
id is not null and not empty
FALSE
xxx is null
FALSE
id is null
TRUE
xxx is not null and not empty
FALSE
id is empty
TRUE
xxx is not null and not empty
FALSE
id is null
FALSE
xxx is null
FALSE
id is null
FALSE
xxx is empty
FALSE
id is empty
FALSE
xxx is null
FALSE
id is empty
FALSE
xxx is empty

BigQuery Replacing NULL with a string

I am new to BigQuery. I have the following simple query:
SELECT name, assetType,resource.data.shieldedInstanceConfig.enableSecureBoot FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
It gives me the the following output:
Row name assetType enableSecureBoot
1 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-b/instances/ddi-us-east4-b-5c2b51b2e3740 compute.googleapis.com/Instance null
2 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-a/instances/ddi-us-east4-a-5c2b51b2e3740 compute.googleapis.com/Instance null
3 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-a/instances/ddi-us-east4-a-f2f9a153c4590 compute.googleapis.com/Instance null
4 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-c/instances/krp-pci-krp-vm-hx5q compute.googleapis.com/Instance true
5 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-b/instances/krp-pci-krp-vm-bvt2 compute.googleapis.com/Instance true
6 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-c/instances/orn-test-01 compute.googleapis.com/Instance true
7 //compute.googleapis.com/projects/blah-web-ingress-prod/zones/us-east4-c/instances/instance-1 compute.googleapis.com/Instance true
8 //compute.googleapis.com/projects/blah-network-hub/zones/us-central1-a/instances/orn-test-central1 compute.googleapis.com/Instance true
9 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/orn-test-east4 compute.googleapis.com/Instance true
10 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/test-vm compute.googleapis.com/Instance true
11 //compute.googleapis.com/projects/blah-network-hub/zones/us-central1-a/instances/test-vm-gui compute.googleapis.com/Instance true
12 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/orn-test-east4 compute.googleapis.com/Instance true
I want to replace the values under field 'enableSecureBoot' with 'Not_Set' I tried IFNULL, as well as IF statements, but gives me unrecognized name 'enableSecureBoot' no matter what I do. Can you anyone give me a hint on how to do it, please?
Thanks so much!!
By looking at your SELECT statement, I think resource.data.shieldedInstanceConfig is a STRUCT in your table.
if you want to create a new column enableSecureBoot using the value of a STRUCT field resource.data.shieldedInstanceConfig.enableSecureBoot,
you could do below
SELECT
name,
assetType,
IFNULL(
CAST (resource.data.shieldedInstanceConfig.enableSecureBoot AS STRING),
'Not_Set'
) AS enableSecureBoot
FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
Please note that the original resource.data.shieldedInstanceConfig.enableSecureBoot is a BOOLEAN type column, and the new enableSecureBoot is a STRING type column
This is what worked:
SELECT name, assetType,IFNULL(CAST(resource.data.shieldedInstanceConfig.enableSecureBoot AS STRING), 'NOT SET') AS ENABLED
FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
Thanks again!
Alex

How can I make IF without ELSE on SQL WHERE condition?

I`m trying to make a querie that selects users and if user type equals 1 I need to select those with age. My table:
id (int 11) | type (int 11) | email (varchar 25) | age (int 11)
My querie:
SELECT * FROM users WHERE IF(type = 1, age <> 0)
The problem is that I need to have an ELSE condition, but I dont need one in this case. How can I make an IF inside WHERE without else condition?
Thanks
You can do it with CASE:
SELECT * FROM users
WHERE age = CASE WHEN type <> 1 THEN age ELSE 0 END
Q: How do I make IF without ELSE on SQL WHERE condition ?
A: It's not possible; there is always an ELSE. MySQL IF() function has three arguments. It doesn't matter where the IF() function is used, whether it's part of an expression in a WHERE clause, or an expression in the SELECT list.
As an alternative to the MySQL IF() function, we can use a more portable, more ANSI-standard compliant CASE expression. But that doesn't get away from the crux of the question, about avoiding an ELSE. There is always an ELSE with the CASE expression as well. If we omit the ELSE clause, it's the same as if we had specified ELSE NULL.
As an aside (unrelated to the question that was asked), I don't think we should be storing age as an attribute; typically age is the difference between the current date and a date in the past (date of birth, registration date, etc.)
I'm thinking we don't need an IF function in the WHERE clause. (That's specific to MySQL, so this answer assumes that the target DBMS is MySQL, and not some other RDBMS).
We can use a combination of conditions, combined with NOT, AND, OR and parens so specify an order of operations.
Sample data and example output goes a long way to explaining the spec.
id type age email
-- ---- ---- ----------
1 0 0 1#one
2 1 0 2#two
3 0 1 3#three
4 1 1 4#four
5 0 NULL 5#five
6 1 NULL 6#six
7 NULL NULL 7#seven
8 NULL 0 8#eight
9 NULL 1 9#nine
Which of these rows should be returned, and which rows should be excluded?
Here is an example query (MySQL specific syntax) that returns all rows except row id=2 (type=1, age=0)
SELECT u.id
, u.type
, u.age
, u.email
FROM user u
WHERE NOT ( u.type <=> 1 )
OR NOT ( u.age <=> 0 )
If there's a requirement to incorporate IF functions, we can do that, and return an equivalent result:
SELECT u.id
, u.type
, u.age
, u.email
FROM user u
WHERE NOT ( IF( u.type <=> 1 ,1,0) )
OR NOT ( IF( u.age <=> 0 ,1,0) )
^^^ ^^^^^
In the WHERE clause, an expression will be evaluated as a boolean value. A numeric value of 0 is FALSE, a non-zero value is TRUE, and NULL value is (as always) just NULL.
For a row to be returned, we need the expression in the WHERE clause to evaluate to a non-zero value (to evaluate to TRUE).
The third argument of the IF() function is the "else" value; for that value, we can return TRUE, FALSE or NULL. To exclude rows that do not satisfy the type=1 condition, we return either zero or NULL:
WHERE IF(type = 1, age <> 0 ,0 )
^^
or equivalently:
WHERE IF(type = 1, age <> 0 ,NULL )
^^^^^
If we want rows that don't satisfy type=1 condition to be returned, we can return any non-zero value:
WHERE IF(type = 1, age <> 0 ,42 )
^^^
RECAP:
Addressing the question that was asked:
Q: How do I make IF without ELSE on SQL WHERE condition ?
A: There is always an ELSE value with the MySQL IF() function; in the context of the WHERE clause, the value will be evaluated as a boolean: TRUE, FALSE or NULL.
I think you want:
SELECT *
FROM users
WHERE type <> 1 OR age <> 0;
I was in a similar situation and ended up with the following solution:
SELECT * FROM users WHERE IF(type = 1, age <> 0, 1=0)
The else part here is 1 = 0 which is never true, so you don't select anything in that case.

How to find all data for date in a database, if criteria is met

I am not sure how to go about doing this. I have never tried an IF statement in SQL.
I would like to Pull all data for the date, ONLY if the where clause is true.
SELECT Distinct Name,Date,ID, Modifier
FROM Database
WHERE Date Between'09/01/16' and '03/31/17'
and ID in ('1','2','3','4')
and Modifier In ('A','B','C')
OR ID in ('1','2','3','4')
And Modifier IS NULL
Example
Name Date ID Modifier
Ted 01/01/01 1 A = True
Ted 01/01/01 7 F = True
Ted 02/02/02 7 F = False
Ted 03/03/03 7 F = True
Ted 03/03/03 1 NULL = True
Any Help Would is appreciated.
Would this be easier with a excel fomula?
I suspect you intend conditions like this:
WHERE Date Between '2016-09-01' and '2017-03-31' and
ID in (1, 2, 3, 4) and
(Modifier In ('A', 'B', 'C') or Modifier IS NULL)
Notes:
- Use standard date formats, such as YYYYMMDD or YYYY-MM-DD.
- Don't use single quotes for numeric values (I assume id is a number).
- I am guessing that your logic wants the first two conditions and then modifier if it is A, B, C, or NULL.

unusual sql server query result

Let's say I have a table called nameAge:
ID Name Age
1 X 12
2 Y 12
3 null null
4 Z 12
and when I run a query like:
select * from nameAge where Age <> 12
it returns me an empty result set while I have row with id 3 where age is different than null?
Using Sql Server 2008 R2.
Any ideas?
Edit: Possibility to be duplicate with suggested answer may be at one point but does not cover at all and it shows how to use null values when compared with null but what I wanted to ask was about the result set which includes null values
This is the intended behavior. You cannot compare NULL values using = or <>. You have to use IS NULL or IS NOT NULL.
If you want NULL values only use IS NULL:
select * from nameAge where age IS NULL
If you want NULL values with age <> 12 values, use:
select * from nameAge where age <> 12 OR age IS NULL
The expression
WHERE NULL <> 12
does not return TRUE or FALSE, but actually returns UNKNOWN. This means that the third record in your table will not be returned by your query.
As #ughai mentioned, you should use IS NULL instead to query that record:
SELECT * FROM nameAge WHERE age IS NULL
Have a look at the Microsoft SQL Server documentation for more information.
When you are dealing with NULLs you should be always careful because of 3 valued logic used in Sql Server(when a predicate can be evaluated to TRUE, FALSE or UNKNOWN). Now here is a classic select statement where many newcomers make a mistake, suggesting that the statement would return all rows where Age <> 12 including NULLs.
But if you know the easy fact that comparing NULL to any value, even to NULL itself will evaluate to UNKNOWN it is getting more clear what is going on. WHERE clause will return ONLY those rows where predicate is evaluated to TRUE. Rows where predicate evaluates to FALSE or UNKNOWN will be filtered out from resultset.
Now let's see what is going on behind the scene. You have 4 rows:
ID Name Age
1 X 12
2 Y 12
3 null null
4 Z 12
and the predicate is:
where Age <> 12
When you evaluate this predicate for each row you get:
ID Name Age Evaluation result
1 X 12 FALSE --(because 12 <> 12 is FALSE)
2 Y 12 FALSE --(because 12 <> 12 is FALSE)
3 null null UNKNOWN --(because NULL <> 12 is UNKNOWN)
4 Z 12 FALSE --(because 12 <> 12 is FALSE)
Now remember that WHERE clause will return only rows where predicate evaluates to TRUE and it is clear that you will not get any result because no row evaluates to TRUE.