ISNULL(Column1,Column2) equivalent in MS Access? - ms-access

I am trying to get something equivalent to ISNULL in Microsoft Access.
I have the NZ function, but I'm not sure how to make it accept a Column name.
SELECT[BANK],[AMOUNT] FROM[BANKDETAILS] WHERE [BANK] = NZ('SBI', BANK]) AND[CITY]=NZ('Delhi', CITY) AND[IFSC]=NZ(4363,IFSC)
How can I achieve my goal?

Just use it like you would IsNull. You're currently reversing the arguments.
The first argument is the column that could be null, the second is the value if it is null:
SELECT [BANK],[AMOUNT] FROM [BANKDETAILS] WHERE [BANK] = NZ([BANK],'SBI') AND[CITY]=NZ(CITY, 'Delhi') AND [IFSC]=NZ(IFSC, 4363)
Do note that comparing columns to themselves is silly, and an ineffective way to test if they're not null. The Nz here has no value.

Related

MySQL don't read second OR condition if the first one is true

I'm trying to make an SQL-statement where I get the parameters from c# code.
The problem is that some of the parameters can be 'null'. But in MySQL I can't properly read WHERE column=NULL and I can only do WHERE column IS NULL.
So I was trying to make the statement like this:
SELECT *
FROM table
WHERE column=param #(param given from C#)
OR column IS param
I thought this would work fine because MySQL shouldn't care about the second condition if the first one is already true (like Python). And it does work when param is null. But there's an error when param is a string because then MySQL reads: WHERE column IS 'string'.
Is there a way to solve this?
You seem to want <=>, the null-safe equality operator:
WHERE column <=> param
Basically, NULL <=> NULL is true, while NULL = NULL is undefined.
Note that you should be properly passing your parameter to the query, using a prepared statement. String 'null' is not the same thing as a null value.

property of non-object to change value to 0

I have a curious question that's going on my mind while looking at my datatables.
I noticed that some columns of my table has no values, like just the column only.
Is it possible when you want to call that column it will return a value of 0?
I tried to call it with a simple SELECT query in sql but its giving me a notice.
Notice: Trying to get property of non-object MySQL result on line (number line)
Can this be used with an if-else statement from the query or a case statement from the query? Hoping for your opinions on this. Thank you :)
Try to use some function like NVL() in Oracle, or IFNULL() in MySQL. This functions set a value when you got a NULL value.

Access query if then argument

The argument I'm looking for is, if the Members.Status field is equal to LA and the Member.Isresident field is False then the Members.Locality field will fill "LOST" in that field. I attempted to write it this way and am receiving an error of invalid syntax.
Locality: Iif ([Members.status] = "LA" and ([isresident] "False", [members.locality], "LOST")
The parentheses in your example are unbalanced. There are two ( but only one ).
Add an equal sign between [isresident] and "False". And if isresident is Yes/No data type, eliminate the quotes around False.
Re-using the field name as the alias for an expression can get you into trouble. You can avoid trouble there with a different alias such as adjusted_locality instead of Locality. But if you prefer to keep Locality as the alias, bracket it as in the example below.
Since I don't know about the context where you're attempting to use that expression, I'll suggest you try this simple SELECT to work out the syntax of the IIf expression.
SELECT
IIf(m.Status="LA" And m.isresident=False, m.locality, "LOST") AS [Locality]
FROM Members AS m;
You can create a new query, switch to SQL View, paste in that SELECT statement, and then run it to see whether any errors remain.

Dynamic Values in 'IN' operator

I have a select statement in which the WHERE clause's IN operator. The query works properly as long as some values are passed to question mark (passed from java program). But when there are no values passed, I get a syntax error.
select
this_.categoryAddressMapId as category1_1_0_,
this_.categoryId as categoryId1_0_,
this_.addressId as addressId1_0_
from
icapcheckmyphotos.category_address_map this_ <br>
where
this_.addressId in (
?
)
When there are no parameters passed, I need null set. Please suggest how to modify the where clause. Thanks in advance
Modify your java program. Your choices are to not run the query if there are no addressIds, or ensure that it passes at least one value. If addressId is an integer field, pass -1 or something like that. Personally, I like the first option but it depends on your requirements.
how about doing sometiong like
where (? is null) OR this_.addressId in ( ? )
you may need to add some special treatment to the first part of the OR if your columns does accept NULLS
//pseudo code
...
WHERE 1
if(!null) {
AND this_.addressId in ('stuff')
}

Replace IIf with SWITCH in WHERE clause

I have the following query in MS-Access 2003 and it works OK:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));
I wish to replace the IIf function with the Switch function but whatever I tried didn't work. My best approach is the following:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));
but I get a message
Type mismatch in expression
Please advise!
One difference I can see is that if [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]<1 the nested IIfs will return 4 while the Switch statement will return Null. Check your underlying data to see if that could happen; a Null value might very well mess up the WHERE clause.