What does this SQL statement produce? - sql

I am having trouble finding documentation about this specific statement
SELECT IF (count(f.id)=0,1,0) as flgNew
FROM table f ON ...
WHERE ...
the table is joined with other tables and should return 1 if the entry in f is found or 0 if it has not been found.
So what does IF (count(f.id)=0,1,0) do?

It's basically the same as the following CASE statement:
SELECT CASE WHEN count(f.id) = 0
THEN 1
ELSE 0
END AS flgNew
...
It checks to see if the expression count(f.id) = 0 is true, and returns the value 1 if it is, and 0 if it is not.
You can read more on the IF() function in the official docs here:
https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_if

Related

How i can return records there are not null only if a column have a determined value

I need to make a query that verify:
if type_answer is equal to Multipla Escolha so i only have to return the records that are not null in correct_answer_description_id
If the type_answer is not equal to Multipla Escolha, don’t make this rule.
So, i try this way:
SELECT * FROM book_unit_question
WHERE book_unit_id = 2
AND status = false
CASE WHEN type_answer = 'Multipla Escolha' THEN
correct_answer_description IS NOT NULL
but i'm getting:
ERROR: syntax error at or near "CASE"
I don't think CASE is the easiest approach here. Can you try the re-written query below?
SELECT * FROM book_unit_question
WHERE book_unit_id = 2
AND status = false
AND (type_answer is null or type_answer != 'Multipla Escolha' or
correct_answer_description IS NOT NULL)
Getting rid of the negative yields negative construct "type_answer is not equal to Multipla Escolha, don’t make this rule" by rephrasing "type answer equal Multipla Escolha then apply this rule. Also applying "only return the records that are not null in correct_answer_description_id". We arrieve at:
select *
from book_unit_question
where book_unit_id = 2
and status = false
and type_answer = 'Multipla Escolha'
and correct_answer_description is not null;

SQL Server LEFT binary equals wrong value

I'm trying to understand what is going on with the following LEFT equality check.
select
CASE
when LEFT(0x000012CA,4) = cast(4842 as varbinary(4)) then 1
ELSE 0
END
Result: 1
Expected: 0
This should come out as 0 but will return 1. If I change 0x000012CA to any other value (but 0x000012EA which is 4842) the result correctly resolves to 0.
It appears that I can do
cast(LEFT(0x000012CA,4) as varbinary(4))
And it resolves properly, but that doesn't explain why say switching 4842 for 4841 works or 0x000012CA to 0x000012BA
Try an excessively larger number to see if it works.
select
CASE
when LEFT(0x000012CA,4) = cast(4842 as varbinary(400)) then 1
ELSE 0
END

how to prevent converting the text into boolean by the use of when statement in postgresql?

select fti.pa_serial_,fti.homeownerm_name,fti.ward_,fti.villagetole,fti.status,
ftrq.date_reporting, ftrq.name_of_recorder_reporting,
case
when fti.status='terminate' then ftrq.is_the_site_cleared ='1' end as is_the_site_cleared from fti join ftrq on ftrq.fulcrum_parent_id = fti.fulcrum_id
Here, is_the_site_cleared is text type of column which is converted into boolean by the when statement written and hence does not print as 1 and takes as true. I explicitly used print '1'. But this also did not work. My aim is to display '1' in the column 'is_the_site_cleared' when the value of fti.status='terminate'. Please help!!!
How about using integers rather than booleans?
select fti.pa_serial_, fti.homeownerm_name, fti.ward_,
fti.villagetole, fti.status, ftrq.date_reporting,
ftrq.name_of_recorder_reporting,
(case when fti.status = 'terminate' -- and ftrq.is_the_site_cleared = '1'
then 1 else 0
end) as is_the_site_cleared
from fti join
ftrq
on ftrq.fulcrum_parent_id = fti.fulcrum_id ;
From the description, I cannot tell if you want to include the condition ftrq.is_the_site_cleared = '1' in the when condition. But the idea is to have the then and else return numbers if that is what you want to see.

CASE in WHERE clause returns Error

I'm running this code:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%' AND
CASE src
WHEN 0 THEN
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
END
I get this error:
Incorrect syntax near the keyword 'IN'
I have no idea what's wrong.
A CASE statement is not appropriate in this case. Just use a simple OR condition:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND (
src <> 0 -- add a COALESCE here if src can be NULL
OR uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
)
... which basically is the equivalent of only applying the uID filtering if src = 0, which is what you appeared to be trying to accomplish with your query.
It sounds like you don't have to use CASE please test this
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND ( (src = 0 --scr is 0 => must have uid in the list
AND UID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481))
OR src <> 0) --else src is not 0 and there is no additional condition
i have a doubt on your not like condition have you tested it alone?
Guess this is what you are looking for
WHERE aIDs NOT LIKE '%''101''%' or
(
src = 0
AND
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481
)
It looks like your statement is not formatted properly. you placed a condition rather than a value to set within "When 0 then ...... END
the uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481) you have there is a condition and shouldn't have been there
The when part of a case statement should select a single value. what you are trying to do is to check for a condition.
Or if you are checking for uid in those values, you should do
case when src = 0 then
case when uID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
then 'It is a Client ID'
-- add another when or else part here if required
end
else 'Not a UID and not a Client ID'
end

Result of request SQL Server

I have the following request that must return result :
select ass.*
from CTR_ASSURANCE ass
inner join CTR_ARTICLEASSURANCE ca on ass.CODE_CONTRAT = ca.CODE_CONTRAT
WHERE
(GETDATE() between ass.DATE_DEBUT and ass.DATE_FIN)
and ass.resilie <> 1
and ca.CODE_ARTICLE = 39
in database there is a row that satisfies this condition but the request doesn't return any result, the problem is in ass.resilie <> 1. This column is numeric and all rows have 'resilie' null
help Please
You can use the IS operator to compare with NULL. Replace
ass.resilie<>1
with
(ass.resilie IS NULL OR ass.resilie <> 1)