Error validating formula for calculated column - sql-server

I'm trying to make up a calculated column that evaluates an existing column with a CASE statement. The following code works fine:
CASE
WHEN Technician_Production_date IS NULL THEN 1
WHEN Technician_Production_date IS NOT NULL AND Technician_Review_Date IS NULL THEN 2
WHEN Technician_Production_date IS NOT NULL AND Technician_review_Date IS NOT NULL AND
Chemist_Review_Date IS NULL THEN 3
ELSE 4
END
When i try to do the same statement but assign an existing column as a VARCHAR value instead as below, i get the error: "Error validating formula for (column_name)". Here is the code im trying to run:
CASE
WHEN Technician_Production_date IS NULL THEN CAST(Responsible_Technician_Review AS varchar)
WHEN (Technician_Production_date IS NOT NULL AND Technician_Review_Date IS NULL) THEN
CAST(Responsible_Chemist_Review AS varchar)
WHEN (Technician_Production_date IS NOT NULL AND Technician_review_Date IS NOT NULL AND
Chemist_Review_Date IS NULL) THEN CAST(Responsible_Chemist_Approval AS varchar)
ELSE "Finished")
END
What could be causing this?

Related

My case statement is returning 1900-01-01 for empty dates

I am trying to return a result for 2 date columns based on different values like this:
CASE WHEN game_startA IS NOT NULL THEN game_startA
WHEN game_startB IS NOT NULL THEN game_startB
ELSE ''
END AS 'Game Start Date'
,
CASE WHEN game_endA IS NOT NULL THEN game_endA
WHEN game_endB IS NOT NULL THEN game_endB
WHEN COALESCE(game_endC, game_endD) IS NOT NULL THEN COALESCE(game_endC, game_endD)
ELSE ''
END AS 'Game End Date'
The problem is, if the value is NULL, it is returning 1900-01-01 instead of saying NULL or just blank.
Is there a way to fix that?
This logic could be vastly simplified (unless there is more to it than you've shared):
COALESCE(game_startA, game_startB) AS [Game Start Date],
COALESCE(game_endA, game_endB, game_endC, game_endD) AS [Game End Date]
I don't see any need for the CASE expressions or any ELSE that tries to turn a date into an empty string. If COALESCE() gets to the end of its list and still doesn't find a non-NULL value, the output is what you want: NULL.

OBJECT_CONSTRUCT function is not working properly

output--
I have written the query in snowflake to generate Json file, from the query output want to remove fields which has NULL. OBJECT_CONSTRUCT is not working properly for some column its not passing NULL value where else for some column its giving null value as result.
Input-
Json remove any field which has value NULL or blank.
{"DIFID":122,"DIF_FLAG":"NULL","DIF_TYPE":"asian/white","FOCAL_COUNT":2370,"REFERENCE_COUNT":17304},
Required Output-
Json remove any field which has value NULL or blank.
{"DIFID":122,"DIF_TYPE":"asian/white","FOCAL_COUNT":2370,"REFERENCE_COUNT":17304},
query-
select distinct ITEMSTATID,object_construct(
'DIFID',DIFID,
'DIF_TYPE',DIF_TYPE,
'DIF_FLAG',DIF_FLAG,
'FOCAL_COUNT',FOCAL_COUNT::integer,
'REFERENCE_COUNT',REFERENCE_COUNT::integer,
'DIF_METHOD',DIF_METHOD,
'DIF_VALUE',DIF_VALUE)
DIF
from DEV_IPM.STAGEVAULT.DIF_STATISTICS;
For string column and 'NULL' as string literal column's value is not skipped:
CREATE OR REPLACE TABLE DIF_STATISTICS
AS
SELECT 1 AS ITEMSTATID,
122 AS DIFID,
'NULL' AS DIF_FLAG, -- here
'asian/white' AS DIF_TYPE,
2370 AS FOCAL_COUNT,
17304 AS REFERENCE_COUNT;
Output:
The value is definitely stored as TEXT:
SELECT null AS DIF_FLAG, 'NULL' AS DIF_FLAG;
On the left: true NULL on the right: NULL string
If it the case then it should be nullified NULLIF(DIF_FLAG, 'NULL') before passing to OBJECT_CONSTRUCT function:
SELECT ITEMSTATID,
object_construct(
'DIFID',DIFID,
'DIF_TYPE',DIF_TYPE,
'DIF_FLAG',NULLIF(DIF_FLAG, 'NULL'),
'FOCAL_COUNT',FOCAL_COUNT::integer,
'REFERENCE_COUNT',REFERENCE_COUNT::integer) AS DIF
FROM DIF_STATISTICS;
Previous answer before column details were provided (also plausible):
It is working as intended:
NULL Values
Snowflake supports two types of NULL values in semi-structured data:
SQL NULL: SQL NULL means the same thing for semi-structured data types as it means for structured data types: the value is missing or unknown.
JSON null (sometimes called “VARIANT NULL”): In a VARIANT column, JSON null values are stored as a string containing the word “null” to distinguish them from SQL NULL values.
OBJECT_CONSTRUCT
If the key or value is NULL (i.e. SQL NULL), the key-value pair is omitted from the resulting object. A key-value pair consisting of a not-null string as key and a JSON NULL as value (i.e. PARSE_JSON(‘NULL’)) is not omitted.
For true SQL NULL values, that column is ommitted:
CREATE OR REPLACE TABLE DIF_STATISTICS
AS
SELECT 1 AS ITEMSTATID,
122 AS DIFID,
NULL AS DIF_FLAG,
'asian/white' AS DIF_TYPE,
2370 AS FOCAL_COUNT,
17304 AS REFERENCE_COUNT;
SELECT ITEMSTATID,
object_construct(
'DIFID',DIFID,
'DIF_TYPE',DIF_TYPE,
'DIF_FLAG',DIF_FLAG,
'FOCAL_COUNT',FOCAL_COUNT::integer,
'REFERENCE_COUNT',REFERENCE_COUNT::integer) AS DIF
FROM DIF_STATISTICS;
Output:
Probably the data type of the column DIFID is VARIANT/OBJECT:
CREATE OR REPLACE TABLE DIF_STATISTICS
AS
SELECT 1 AS ITEMSTATID,
122 AS DIFID,
PARSE_JSON('NULL') AS DIF_FLAG, -- here
'asian/white' AS DIF_TYPE,
2370 AS FOCAL_COUNT,
17304 AS REFERENCE_COUNT;
Output:

Returning IS NOT NULL in SQL CASE WHEN expression

How can I return IS NOT NULL on from a SQL Server CASE statement?
I have tried something like this:
WHERE
a.BillToCustomerID IS NOT NULL
AND CASE #taxSchedRequiered
WHEN 1 THEN a.TaxScheduleID IS NOT NULL
END
but it is not working for me.
Update
I need to filter by a.TaxScheduleID IS NOT NULL, just in the specific case when the value of the variable #taxSchedRequiered (possible values are: null, 0, 1) is 1.
just change the logic and use an OR
WHERE
a.BillToCustomerID IS NOT NULL
AND (#taxSchedRequiered <> 1 OR a.TaxScheduleID IS NOT NULL)
this will include all records if #taxSchedRequiered = 0 else it will only include records that have a a.TaxScheduleID <> NULL
Also make sure that #taxSchedRequired has a value other than NULL when you execute this
Edit to check for null param
WHERE
a.BillToCustomerID IS NOT NULL
AND (ISNULL(#taxSchedRequiered,0) = 0 OR a.TaxScheduleID IS NOT NULL)

SQL CASE when text field is NULL not working

I'm trying to run the following case statement in SQL:
CASE
WHEN P.Photos_Cloned_From IS NULL
THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
The statements works, but only for those records that are not NULL. For items that are NULL, the statement is not returning the THEN condition.
Any suggestions?
Try this and let us know what happens.
'http://www.toolboxbarn.com/v/vspfiles/photos/'+
COALESCE(P.Photos_Cloned_From,P.ProductCode,'DEFAULT')+'-2T.jpg'
AS image_link,
Using coalesce here is better than the case statement. Some platforms can optimize coalesce and it lets you easily make a default value.
#hogan's Suggestion is a great one to save code, but ultimately it should have the same result as your case statement if you don't introduce his new 'default' case. It is most likely that the ProductCode is also NULL is that a possibility?
Why string + NULL = NULL because NULL is an unknown in sql most db platforms will nullify the entire value when null is aggregated or concatenated.
So Think of the following test cases:
Id ProductCode Photos_Cloned_From
1 1 ClonedFrom
2 2 Null
3 NULL Null
The results of your case expression and Hogan's Suggestion would be:
1) ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/ClonedFrom-2T.jpg'
2) WHEN 'http://www.toolboxbarn.com/v/vspfiles/photos/2-2T.jpg'
3) WHEN NULL
CASE WHEN ISNULL(P.Photos_Cloned_From,'') = '' THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
SELECT
F.Name
, F.Address
, F.InDate
, ( CASE WHEN (F.Date_Down IS NULL and F.Cod_Down = 0 )THEN 0 ELSE -1 END) as 'sn_Down'
FROM
Folders F

Handling NULL value in ORACLE Table Query

I have a question regarding handling NULL value in a column in ORACLE Table.
So, when i query a table, i get this error message in every NULL value occurences
Notice: Undefined index: STATUS in C:\xampp\htdocs\WeltesInformationCenter\AdminLTE\pages\tables\assignmenttable.php on line 481
my query is like this
SELECT MASTER_DRAWING.*, (SELECT PREPACKING_LIST.PACKING_STATUS FROM PREPACKING_LIST WHERE MASTER_DRAWING.HEAD_MARK = PREPACKING_LIST.HEAD_MARK) STATUS FROM MASTER_DRAWING WHERE PROJECT_NAME = :PROJNAME
My question is, how to handle NULL value so that when it sees a null value, it can return some value such as 0 or any string.
Thanks
Try
SELECT MASTER_DRAWING.*,
NVL((SELECT PREPACKING_LIST.PACKING_STATUS
FROM PREPACKING_LIST
WHERE MASTER_DRAWING.HEAD_MARK = PREPACKING_LIST.HEAD_MARK),'N/A'
) STATUS
FROM MASTER_DRAWING WHERE PROJECT_NAME = :PROJNAME

Resources