How do return a Constant Value in the Sql Else Clause when only on value remain's the same - tsql

UPDATE ConcordWholesales_new.dbo.Product
SET CCCQTYINSTOCK=isnull(#newQTYINSTOCK,'')
where SKU=#newSTOCKCODE
UPDATE Product
SET Published = CASE WHEN CCCQTYINSTOCK=isnull(#newQTYINSTOCK,'')
THEN '1' ELSE 0 END
I have an Update Trigger that Update's Stock Quantity's when its below 0 the Product becomes Unpublished. If the StockQuantity goes above 0 the above Case Updates the Published feild to republish the Product.
When I update the ELSE clause returns a 0 value on the other columns, but If I remove the ELSE clause it can't insert a null value.
How do I return the above code so it only Updates one line of data.

you were missing WHERE clause in second statement
UPDATE Product
SET Published = CASE WHEN CCCQTYINSTOCK > 0
THEN 1 ELSE 0 END
where SKU=#newSTOCKCODE
and one more thing, what datatype is Published column ?
you are using '1' - its nchar, and 0 - int

Related

replacing blank with zero sparksql

i have created a query that returns users who placed order in a specific month and after how many months they have returned. the problem is when there's no user placed any order in a month it returns blank . I want to return zero instead .
I created a case statement as :
case when user_id is null then 0 else user_id end
I also tried:
case when user_id is null and delivery_month is null and returned_month is null then 0 else user_id end
if there's no row evaluated for the user, how can i replace that with 0 ?
this is how blank values looks like when I loaded data in the visualization tool:

Data Conversion error in SQL, unsure what field it could be referring to

Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to numeric.
Below is the code that I am using and the schema for the fields mentioned in the code. Some context behind this, I have a set list of indcodes. I only give one below but there are 350 in total. It is quite likely when i query by indcode, area, and ownership that the data does not exist. Hence, why I have the isnull language. Finally, I need rows that contains a value of suppress = 1 to show up as N/A.
For every indcode that I query, I need a result. The result could be actual data or some text (NULL, N/A, etc).
Last is the intended result. I only gave code for one indcode but have included six to give the example some depth.
select
CASE
WHEN suppress = 0 then isnull (mnth1emp, 0)
ELSE 'N/A'
END as mnth1emp,
CASE
WHEN suppress = 0 THEN isnull (mnth2emp, 0)
ELSE 'N/A'
END as mnth2emp,
CASE
WHEN suppress = 0 THEN isnull (mnth3emp, 0)
ELSE 'N/A'
END as mnth3emp
from dbo.industryimport20172f
where area='000003' and indcode='21' and ownership='00'
suppress char (1)
mnth1emp numeric(9,0)
mnth2emp numeric(9,0)
mnth3emp numeric(9,0)
area char(6)
indcode char (6)
ownership char(2)
123 456 789
1 2 3
Null null null
2 3 4
3 4 5
Null Null Null
So the first things that jump out here are:
a) While you're asking for numbers to be returned in the case statement, you're then asking for a string when you get to ELSE. Try casting all the fields as varchars and then entering them as strings:
CASE
WHEN cast(suppress as varchar) = '0' then isnull (cast(mnth1emp as varchar), '0')
ELSE 'N/A'
END as mnth1e
You may not need to cast suppress.
b) Does supress ever have anything other than a number in it? If so, you would need to put the zero as a string:
CASE WHEN supress = '0' --etc.

Count the number of occurrences of Null in a column in tableau

I am relatively new to Tableau and I am wondering if there is a way to calculate null values in a column.
I have a column called Email of type string and want to know how many people have not entered their email i.e. Null.
I tried to create a calculated field with
count(ISNULL([Email]))
But this gives me the total count and not the count of null.
Thanks.
You cannot count NULL since COUNT ignores NULLs.
You can do this, though:
SUM(IF ISNULL([Email]) THEN 1 ELSE 0 END)
Per your additional comment, if you wanted to count where two fields are both NULL then:
SUM(IF ISNULL([Email]) AND ISNULL([Phone]) THEN 1 ELSE 0 END)
You can continue this for any number of fields, as needed.
You can use a filter for that field. Set it to only show you Null values, and set the measure to Count instead of the default Sum.
You can create another column where all null values are set to 1 and anything else to 0, >then count that. It should be something like
>COUNT(IF "null" THEN 1 ELSE 0)
A working syntax for that would had been:
SUM(IF ([Your field] = null) THEN 1
ELSE 0
END)
You can create another column where all null values are set to 1 and anything else to 0, then count that. It should be something like
COUNT(IF "null" THEN 1 ELSE 0)

Add value of subquery in Entity Framework

I have a really strange situation with the subquery.
Value of subquery is calculated but assigned value to property is different
var ozForAllViews = from oz in dbContext.oz
where
oz.TId == 6050
select
new ozForAllView
{
ozId = oz.ozId,
Ilosc = oz.Ilosc - (from pz in dbContext.pz
where
pz.Aktywny &&
pz.ozId == oz.ozId
select pz).Sum(z=> (decimal?) z.Ilosc) ?? 0
};
My property Ilosc is not calculated properly, value in DB equals 5.
But returned value is always 0.
Why is that oz.Ilosc is not subtracted from subquery?
I mean that 5 - 0 should equals 5.
You should surround the expression that calculates the subtracted amount by parentheses:
from oz in dbContext.oz
where oz.TId == 6050
select
new ozForAllView
{
ozId = oz.ozId,
Ilosc = oz.Ilosc - ((from pz in dbContext.pz
where
pz.Aktywny &&
pz.ozId == oz.ozId
select pz).Sum(z=> (decimal?) z.Ilosc) ?? 0)
};
This is because the query is translated into SQL. In SQL, if one part of an expression is null, the whole expression is null. Your query is translated such that oz.Ilosc - (from ... (decimal?) z.Ilosc) is evaluated as one expression. That part becomes null when the subtracted amount is null and hence it will be returned as 0.
It's confusing because in plain C# code, the behavior would be different. There the ?? 0 part would be applied to the subtracted amount only.

How to return zero for a field in Crystal report

I am generating crystal report from database. My requirement is to when the field TKK Balance returns 0 or more than 0 for a row the report will print a dash '-' for that row and if it is less than 0 then only it will return the original value from the database. Is there anyone who can help me on this? please?
Thank you
First of all it can be done inside the SQL select statement like this
,(
CASE WHEN [TKKBalance] >= 0
THEN '-'
ELSE CONVERT(NVARCHAR, [TKKBalance])
END
) AS [SomeName]
or, you can create and use a formula field inside your report with the following code
if {TableName.TKKBalance} >= 0 then
"-"
else
totext({TableName.TKKBalance})