I am trying to do -
Where doctype is not null (doctype=documenttype)
I had a look at the SQL on MSDN for not null and where clauses and i cannot seem to find anything.
My last attempt was
where DocType is not null (f4111dt.DocType=f0911.documenttype)
But it is throwing up an error around f4111dt which is not an issue
If i just have where 4111dt.DocType=f0911dt.documenttype
then i lose the null values (which i want) which are in 4111dt.DocType because they do not match to anything in f0911dt.documenttype.
However if i dont have the statement 4111dt.DocType=f0911dt.documenttype
The document types wont match up...
In case you are trying to bring also those where doctype is null:
Where DocType is null OR (f4111dt.DocType=f0911.documenttype)
Try this
where DocType is not null AND (f4111dt.DocType=f0911.documenttype)
Related
I have written a report and im having a little bother with NULL values.
In my report i had a WHERE clause:
WHERE Cust.Ref LIKE ?
This clause caused issues with another section of my report (Nominal Codes). When i filtered on a nominal code, it returned 0 results where it should have returned 34 results.
I removed the WHERE clause and it worked fine...Great i have found the issue.
I changed my WHERE clause to:
WHERE (Cust.Ref LIKE ? OR Cust.Ref IS NULL)
This fixed the nomincal code issue but now when i filter by a customer reference it also brings back all NULL values.
Can someone please advise?
Thanks.
You can turn the where clause around a bit to check for a null placeholder value.
WHERE (? IS NULL OR Cust.Ref LIKE ?)
I am trying to check a column which belongs to View "IS NULL" and it works good with select statement:
SELECT TOP (1000) [Invoice_Number]
,[Invoice_Date]
,[Invoice_Amount]
,[Invoice_CoCd]
,[Invoice_vendor]
,[Invoice_PBK]
,[Invoice_DType]
,[Invoice_DueDate]
,[Invoice_ClgDate] FROM [dbo].[viewABC] where Invoice_PBK IS NULL
Now I have an update statement, where I need to update a column in a table based on NULL in VIEW:
UPDATE cis
SET
cis.InvoiceStatus =
(
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD'
THEN '233' END)
FROM
[dbo.[tblABC] cis,
[dbo].[viewABC] imd
WHERE [condition logic]
These is no issue with where condition, the IS NULL in the CASE expression causing the problem.
Can someone help please?
You may want to just test your Logic piece by piece.
Try
SELECT ISNULL(RTRIM(LTRIM(imd.[Invoice_PBK])),'Hey Im Null')
FROM
[dbo].[tblABC] cis,
[dbo].[viewABC] imd
WHERE [condition logic]
See if you get that String value on your returned column(s) from the Query. Then, build from there. It could be something as simple as the JOIN. Which I'm not a fan of that old syntax but, without more info on this table other than [conditions]. It's hard to just guess an answer for you. You have no detailed evidence that helps us. It very well could be your conditions but, you're saying "condition logic" and that does nothing for the group on this thread.
Looking at this expression:
cis.InvoiceStatus =
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
END
And this symptom:
CIS.InvoiceStatus gets updated with NULL
The obvious conclusion is neither WHEN condition is met, and therefore the result of the CASE expression is NULL.
Maybe you wanted this, which will preserve the original value in that situation:
cis.InvoiceStatus =
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
ELSE
cis.InvoiceStatus
END
Or maybe you wanted this, to also match an empty string value:
cis.InvoiceStatus =
CASE
WHEN NULLIF(RTRIM(LTRIM(imd.[Invoice_PBK])),'') IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
END
It's also worth pointing out the two WHEN conditions are looking at two different columns.
Finally, it may be worth a data clean-up project here. Needing to do an LTRIM() will break any chance of using indexes on those fields (RTRIM() is slightly less bad), and index use cuts to the core of database performance.
So hopefully this isn't a super difficult question. I've looked around but haven't been able to find an answer. Basically I have a table in a READ-ONLY db that I'm trying to use a case statement to convert a column that stores JSON values (I think?) to something more visually pleasing.
The table shows Emails, and the Status of whether or not they're subscribed to the mailing list.
EXAMPLE HERE
Basically my query looks something like this
select
f.data as "Email",
(
case f.status
when '{"value":true}' then 'Yes'
when '{"value":false}' then 'No'
else NULL
end
) as "Subscribed"
from fields f
When I run this in my example page it works just fine when set to POSTGRES 11 but when I run it on Metabase, I get an error "ERROR: operator does not exist: json = unknown" and I'm stumped on how to proceed.
Any help here would be greatly appreciated.
That error means f.status is of type json and you're trying to compare it to a string, which doesn't work.
You can try the following instead (related documentation):
case f.status->>'value'
when 'true' then 'Yes'
when 'false' then 'No'
end
as "Subscribed"
My field in my SKU table
(BI.dbo.SKU.phl5) is varchar(15)
However below code returns just 3 characters 'Unc' for the null fields in my table while it should return 'Uncategorized'. How to solve that?
ISNULL(SUBSTRING(BI.dbo.SKU.phl5,0,3),'Uncategorized') AS phl1
ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,3) AS VARCHAR(13)),'Uncategorized') AS phl1
The size of the return type of SUBSTRING isn't clearly documented that I can find, but the problem is that the type of ISNULL is the type of the first expression, which is clearly coming back as VARCHAR(3) since you are truncating it to 3 characters.
ISNULL docs
Try this
CASE WHEN BI.dbo.SKU.phl5 IS NULL THEN 'Uncategorized'
ELSE SUBSTRING(BI.dbo.SKU.phl5,0,3)
END AS phl1
The issue is i have 3 conditions good bad ugly. My goal is if one of the following condition is null then replace null to 0 else if all the conditions are null then show it as null
case when 'Good' is not null or 'Bad' is not null or 'Ugly' is not null
then coalesce(value,0)
else value
end result
The problem here is even with the condition it turns all the nulls to 0 which is not what i want. Thanks in advance
Your current expression says (in pseudocode):
IF <something is true>
IF value IS NULL
RETURN 0
ELSE
RETURN value
ENDIF
ELSE
RETURN value
ENDIF
Is that what you wanted?
I think maybe you want something like this. If I get more details I will update.
If I were just going off of this comment
the question is if good is null and bad and ugly for the same product
has a value 0-100 then i want the null to be replaced to 0. if all the
three are null for the same prod then i want them to be nulls and not
0
I would give you something like this
case
when good is not null then good
when good is null and coalesce(bad,-1) between (0 and 100) and coalesce( ugly,-1) between (0 and 100) then 0
else null
end
If you were trying to provide all 3 columns (I am assuming you have multiple columns) you might do something like this.
select
case
when good is null and bad is null and ugly is null then null
when good is null and (bad is not null or ugly is not null) then 0
else good
end as 'good column values'
,
case
when good is null and bad is null and ugly is null then null
when bad is null and (good is not null or ugly is not null) then 0
else bad
end as 'bad column values'
,
case
when good is null and bad is null and ugly is null then null
when ugly is null and (good is not null or bad is not null) then 0
else ugly
end as 'ugly column values'
Your code says "if one of good, bad, or ugly is not null" but your question says "if one of good, bad, or ugly is null"
If I'm understanding you correctly, just take out the nots.
Edit: I'm also assuming that you don't actually mean to use the literal strings "Good", "Bad" and "Ugly", but that they are meant to represent column names or something.
You should be aware that using these strings means that your code will never enter the "else" condition, as "Good", "Bad" and "Ugly" are not null.
EDIT 2: take your column names (good, bad, ugly) out of quotes so they will actually be evaluated. This is why you're still seeing that error.