Anonymize data in select statement - sql

We have a requirement to anonymize user columns, so they show NULL instead of the username.
Though of doing this
case when 1 = 1 then NULL else NULL end as USERNAME
But of course this doesn't work: At least one of the result expressions in a CASE specification must be an expression other than the NULL constant.
Is there a cleaner way to anonymize data in a select statement?

Related

DB2 SQL Statement WHERE clause CASE WHEN in multiple conditions

This is what I am trying to do:
SELECT
id, name
FROM
users
WHERE
isActive=true
(AND CASE WHEN {param} != null THEN name={param} ELSE null END)
if the passed {param} is not null then only the AND operator will be added otherwise just isActive=true condition will be used.
You can use something like COALESCE or (in case of DB2) NVL.
SELECT
id, name
FROM
users
WHERE
isActive=true
AND name=COALESCE({param},name)
You didn't say how you pass parameters/variables, so i'll leave it to you. "{param}" could be replaced with a column or constant in this example
try this
SELECT
id, name
FROM
users
WHERE
isActive=true
AND (({param} is not null AND name={param}) OR ({param} is null))

Not in clause on a column with Null values

I have a table with 10 odd columns, one of them being 'Status'.
I wanted to fetch all rows where Status is not Rejected, so I wrote the following query on Hive:
select * from table1 where status <> 'Rejected'
However Hive is not returning me rows where the Status was Null. I changed the query to
select * from table1 where status <> 'Rejected' or status is Null
But I can't find any documentation to understand why this is happening.
Can someone please help me with this?
Hive implements the NULL-safe comparison operator. So you can do:
select *
from table1
where not status <=> 'Rejected' ;
As for your question, it is a pretty basic question on what NULL means in databases. It doesn't mean "missing", it means "unknown". Almost all comparison operations return NULL when either operand is NULL -- the exceptions are operands (such as <=>, is not null and is null) that are specific designed to handle NULL values.
null isn't a value, it's the lack thereof. Whenever you try to use it in a context of the value, the result would be "unkonwn". You can think about it like this - "is an unknown (=null) value different from 'Rejected'? We don't know."
Thus, you need to specifically handle it with the is [not] operator. You can think of the second where clause you shared as "all the statuses that are not known to have the value 'Rejected'".

Select from table with 2 condition and 2 operations

I have a Table BoxTrans
the table Contain Rows (ID,Date,FromBox,ToBox,Value)
I want to make a View like (ID,Date,Box,ValueIn,ValueOut)
select when frombox Give Value to ValueOut
and when tobox Give Value to ValueIN
You can use a CASE statement to check the value of a different column when populating a column. The below query will return your output as long as either ToBox or FromBox is NULL, if they are both not null you may get unexpected results.
SELECT ID,
Date,
COALESCE(ToBox,FromBox) as Box,
CASE WHEN ToBox IS NOT NULL THEN value ELSE NULL as ValueIn,
CASE WHEN FromBox IS NOT NULL THEN value ELSE NULL as ValueOut
FROM BoxTrans

Checking tables for null returns 0 regardless

I don't understand why this query doesn't work.
I have a table which is full of rows where 3 of the column values are set to NULL.
But when I run the following query, it returns 0 (it should return 96)
SELECT COUNT(*) FROM SEAT WHERE BOOKED=null;
Do you know what I am doing wrong?
It depends on your database settings and the specific RDBMS you are using, but if you are using ANSI NULL syntax you cannot directly compare a value to NULL with an equality test (=) -- it will always fail.
Use WHERE BOOKED IS NULL instead.
You have to use IS NULL instead of = null
Since null technically isn't a value, you can't compare null using the = operator.
Use IS NULL
SELECT COUNT(*)
FROM SEAT
WHERE BOOKED IS NULL

Trouble comparing smallint in where clause

I have a table with a column called status of type smallint and accepts null. Sql server 2000.
My data contains mostly 2 in that field, but also 0 and null.
When I do
select *
from table
where status <> 2
I don't get all the proper records (where status is null or 0). Any idea why this is happening and how to correct? Shouldn't <> give me everything other than 2?
The NULL test doesn't match any arithmetic comparison.
use
where status <> 2 OR status is null
select *
from table
where ISNULL(status, 1) <> 2
NULL doesn't compare: so remove it.
Your example is the "common mistake" on Wikipedia too...
What you're expecting can be accomplished by setting ANSI_NULLS to off.
For example, try running these queries:
set ansi_nulls off
select case when 1 != null then 'true' else 'false' end
set ansi_nulls on
select case when 1 != null then 'true' else 'false' end
That being said, this is very non-standard SQL behaviour that you're expecting to see. NULL comparisons should always be considered false whether equals to or not equals to comparisons, as every developer will expect that type of SQL query behaviour.
Your WHERE clause should then look like:
where status <> 2 or status is null
Another option would be just to compare to status = 1, if that is the only status that you're expecting to be included in your query.