If statement for vbnet - vb.net

I want to make an if statement with conditions that have 2 result
Example
if (id = "25" OR "36") then
print "The id is 25 or 36"
else
print "the id is not 25 or 36"
end if
My concern is in the if condition statement for "OR"
I try with "AND" but this only takes id = 25 as true whereas id = 36 as false
I try with "OR" "ORELSE" "XOR" this takes everything as true.
I try || sign but I got syntax error

You are missing the comparision after the or:
If (id = "25" Or id = "36") Then

You need to provide the variable to check each time
If id = "25" or id = "36" Then
Alternatively, if this is likely to expand to include other numbers, you could use
If {"25","36"}.Contains(id) Then

Alternatively, you could use a Select Case statement:
Select Case id
Case "25", "36"
Print("The id is 25 or 36")
Case Else
Print("The id is not 25 or 36")
End Select
This works the same way as an if..else statement, but allows you to easily supply different test expressions.

Related

Conditional Query Using "AS" field

SELECT "public"."mv_tags_per_org"."count" AS "count", "public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
WHERE "NPO???" = "SSS"
Above you can see my code. It is currently returning exactly the output I want when you remove the "WHERE" function. I'm adding the "WHERE" function and attempting to access the new column I made called "NPO". It seems as if the column does not exist to the SQL editor, but it does exist when the query is ran. How do I access it?
Thanks!
Enclose your query into a "table expression" so you can produce a named column. Then you can use it in the WHERE clause:
select *
from ( -- table expression 'x' starts here
SELECT
"public"."mv_tags_per_org"."count" AS "count",
"public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
) x
WHERE "NPO" = 'SSS'
Note: "table expressions" are also called "derived tables" and "inline views" by different teams of people.
The WHERE clause cannot relate to a column alias defined in the SELECT clause (because the former it is evaluated before the latter).
This does not really matter for your use case, which can be simplified as:
SELECT
"public"."mv_tags_per_org"."count" AS "count",
"public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
WHERE "public"."mv_tags_per_org"."ngo_id" = 30

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 - Select dummy field and set value with condition on another field

I neeed to do something like this in PostgreSQL:
SELECT "OFF_ID", "DUMMY" ( <--fake field )
CASE WHEN "OFF_ID" = 10661
THEN
DUMMY value is set to "john"
ELSE
DUMMY value is set to "doe"
WHERE "OFF_STATUS" = TRUE
The " CASE " block above is TOTALLY WRONG, I just can't figure out how to proceed.
In other words, I need to select a fake column (DUMMY), and set the returned value according to a condition, in my example, depending on OFF_ID value.
You need to give the alias "dummy" to the result of the CASE expression:
SELECT "OFF_ID",
CASE
WHEN "OFF_ID" = 10661 THEN 'john'
ELSE 'doe'
END AS dummy
FROM ...
WHERE "OFF_STATUS" = TRUE
You can try below -
SELECT "OFF_ID",
CASE WHEN "OFF_ID" = 10661 THEN 'john' ELSE 'doe' end as Dummy
from tablename
WHERE "OFF_STATUS" = TRUE

Within a Query can you have a result changed to something based on a returned value

Oracle SQL Developer Version 18.2.0.183, Build 183.1748
This request for help comes from a lack of knowledge on the subject. I have a query I am working on and one of the fields in the Query will always return a Value of either "S" or "C". Anytime a "S" is returned I need that field to instead of outputting a "S" to instead output "Residential" and anytime a "C" is returned to instead output "Commercial".
Here is a Sample of the query:
SELECT
DBSTAGE.OAWOM.WORKORDERNUMBER "Work Order Number",
DBSTAGE.OAWOM.BILLTYPECODE "Bill Type Code",
DBSTAGE.OAWOD.SERVICECODE "Line Description"
FROM
DBSTAGE.OAWOM,
DBSTAGE.AOWOD
WHERE
DBSTAGE.OAWOM.WORKORDERNUMBER = DBSTAGE.OAWOD.WORKORDERNUMBER
AND DBSTAGE.OAWOM.SITEID = DBSTAGE.OAWOD.SITEID
This query outputs the following:
"ACTUAL OUTPUT"
And I am wanting it to instead output this:
"DESIRED OUTPUT"
You can use CASE statement in your select to change the field output based on field input value.
Eg
SELECT cust_last_name,
CASE credit_limit WHEN 100 THEN 'Low'
WHEN 5000 THEN 'High'
ELSE 'Medium' END
FROM customers;

VBA equivalent to SQL 'in' function

I'm writing a conditional statement in vba like
if(userID = 1 or userID = 2 or userID = 3 or userID = 4) then
...
I was wondering if there's a quicker, cleaner way to do this. Something like
if(userID in (1,2,3,4)) then
...
Thanks
An alternative would be:
select case userID
case 1,2,3,4,5,6
' do something
end select
It conveys very good the meaning of the if ... then ... else construct.
Another way
If UBound(Filter(Array(1, 2, 3, 4, 5, 6), UserID)) > -1 Then
Filter returns an array with the match. If there's no match, ubound = -1.
You can use the Application.Match function on an array:
If Not IsError(Application.Match(userID, Split("1,2,3,4",","))) Then...
CW because this matches the hypothetical example, but not likely a real use situation. However, Like is a good keyword to know.
If userID Like "[1-6]" Then
This is ok for single digit checks, but not real world multi-character user IDs.
i.e.
userID = 1
If userID Like "[1-6]" Then ' result is True
but
userID = 11
If userID Like "[1-6]" Then ' result is False
You could make a basic function like this:
Function InArray(Match, SourceArray)
InArray = False
For i = LBound(SourceArray) To UBound(SourceArray)
If SourceArray(i) = Match Then
InArray = True
Exit Function
End If
Next
End Function
Then you could say:
if InArray(userId, array(1,2,3,4)) then msgbox "Found it!"