Standard Big query Not removing String - google-bigquery

I'm trying to filter out "null" from a string column using Standard Big query and for whatever reason it is not being filtered out
so my where statement goes:
where d_transaction_dt <> "null"
d_transaction_dt is a string column that I'm trying to cast as date and remove anything in there that is "null"
I'm getting the error: Invalid date: 'null'
Please help..

Is it literally the string "null", or is the field null? If you're doing something like a SAFE_CAST to convert strings to a DATE type, you're probably getting NULL values.
WHERE d_transaction_dt <> "null"
is an entirely different filter predicate than
WHERE d_transaction_dt IS NOT NULL

Related

Is it possible to use a COALESCE function inside a SUM function?

I'm trying to use COALESCE function at postgres in a query like this:
SELECT
id_school,
SUM(COALESCE((student->'name'->>'age'), '0')::numeric)
FROM
teachers
GROUP BY 1;
The problem is at line:
SUM(COALESCE((student->'name'->>'age'), '0')::numeric)
I tried to use COALESCE because I'm receiving some empty strings and the ERROR: invalid input syntax for type numeric: ""
Shouldn't COALESCE solve this casting error? Can't I use COALESCE in a SUM function?
You don't actually need the coalesce() function inside the sum, because sum will ignore NULL values anyway.
You error isn't caused by a NULL value, but by an empty string.
So you need to convert the empty string to a NULL value. This can be done using nullif() in Postgres
SELECT id_school,
SUM(nullif(student->'name'->>'age'), '')::numeric)
FROM teachers
GROUP BY 1;
If you could also have strings with blanks in it the JSON value, you might want to consider nullif(trim(student->'name'->>'age'), '')
COALESCE returns first non-null argument. Empty string is non-null. You are trying to cast '' to numeric value.
You can use NULLIF to get null instead of empty string in COALESCE.
SQL Coalesce with empty string

CASE statement where conditional includes an IN statement redshift

CASE
WHEN code IN ('FJS354', 'JDF334')
THEN 'Lower_form'
ELSE 0
END AS format
This returns an error in Redshift
invalid input syntax for integer: "Lower_form"
I know if I change 'Lower_form' to an integer it will work however I want this column to be a string. Is there a way to do this?
I want this column to be a string.
All branches of a case expression must return the same datatype. You are giving two literal values whose datatype is not the same (string vs integer): the database makes the decision to turn them both to integers - which is not what you want.
Rremove the ambiguity by being explicit about the datatype you want to return. That is, make this literal 0 a string:
CASE WHEN code in ('FJS354','JDF334')
THEN 'Lower_form'
ELSE '0'
END as format

What does this statement mean in SQL? (isnull(cast(field_name as CHAR), '') = ")

I am new to SQL and I am looking at established queries that we have, can anyone explain what this statement means in the WHERE clause
(isnull(cast(field_name as CHAR), '') = '').
Breaking down each statement:
CAST(field_name AS CHAR) converts the field_name column to a CHAR
This value is then passed as the first argument to the ISNULL() with the second being an empty string ''.
ISNULL(CAST(field_name AS CHAR), '')
This takes the result of the cast, and if it's a NULL value, returns '' instead.
Finally, it checks if that result is equal to ''.
Essentially, it's checking for NULL or empty string values in one fell swoop.
juergen d is correct. I'd add that it tries to cast the field to alphanumeric before it checks if it is null or blank. If the field's data type has no defined conversion to type CHAR, the query will error out. Have a look at this regarding conversions/casts.
T-SQL Data Types and Conversion Info

Return null value or numberic value of 2

I'm needing to return values in SQL query that are either null or 2 for broker reason codes. I've tried using a.BROKER_REASON in (2,null), but it only pulls back 2's. I've tried using "a.BROKER_REASON is null or a.BROKER_REASON = 2" and get error msg "Conversion failed when converting the varchar value '+MULTI' to data type int." Is there an easy way to return rows with null values or values of 2?
That error looks more like your value stored isn't an actual number but rather a small string.
Try:
a.BROKER_REASON is null or a.BROKER_REASON = '2'
If not you may have extra spaces there stored, and can happen depending on storage engines/table definitions, in which case you can do a LTRIM or LEFT (depends on what SQL database you use) or equivalent to trim off excess spaces.

In SMSS what's the difference between NULL and nothing, in a query result?

I know this maybe a newbie question, but i'm wondering why I sometimes get NULL in a result, and sometimes it's just totally blank - shouldn't they all be null?
As you can see, the Remarks column has no background color of yellow which indicates that it is not NULL. (NULL values has.)
It contains an empty string '' (or maybe spaces) which is different from NULL because empty string is set as empty while NULL is not set (nothing).
In Oracle, the empty string is treated as NULL. SQL Server treats it as a string value.
The main difference is that in SQL Server you can return results from something like
select * from mytable where myfield = ''