I have a calculated column that always evaluates to true even when it should and I can't figure out why. Any help would be greatly appreciated, here is the column:
=[Name]&" "&IF([num1]>0,"("&[num1]&")","")&" "&IF([num2]>0,"("&[num2]&")","")
So I it should show num1 if it is > 0 and num2 if that is > 0 but it shows them both all the time regardless of their values.
Are you very very sure [num1] and [num2] are both numbers?
Because
=IF("-1">0,...
will evaluate to true
=ISNUMBER([num1])&ISNUMBER([num2])
should return
TRUETRUE
if any of the two return FALSE
there is your problem.
This will cast strings to numbers:
=IF(VALUE(num1)>0,"("
&num1
&")","")
&" "
&IF(VALUE(num2)>0,"("
&num2
&")","")
square brackets are only required when there are spaces in the column names
Related
I am working on an add-query.
In the add query I am trying to fill field C with with a true or false value, depending on the value in field A.
If the value in field A equals -1 the value in field C should be true (-1)
I thought that the solution would be something like the following, but I am getting #Error in the results:
C: IIf([A]='-1',True,False)
A solution that seems to return the desired outcome is the following:
B: IIf(Nz([A])='-1',True,False)
The problem with this (NZ-function) is that it throws an error when running the query with VB (ADO,DAO or OLEDB)
My question is:
What formula can be used to get to the desired results without using the NZ-function
The desired results are as given in field B
Since you're using quotes, I assume your value is a string. In that case, Nz will convert a Null to a zero-length string.
You can achieve the same by simply concatenating an empty string:
IIf([A] & ''='-1',True,False)
A more general solution is to use IIf, which allows you to specify an alternative value on nulls:
IIf(IIf([A] IS NULL, '', [A]) = '-1', True, False)
As [A] is numeric, all you need is to compare it with True:
C: [A]=True
I am attempting to write a nested iif statement in SSRS that will convert the varchar values that are numbers(isnumeric) to integer and leave the text as is. I also want to be sure if the value in the cell is "0" it will return a blank or "". Please see the code I am using below. Any insights as to why the non-numeric text is showing as #Error would be greatly appreciated. Thanks!
=iif(
Fields!O1_Parent_Line_Item_ID.Value is "0", "", iif(ISNUMERIC(Fields!O1_Parent_Line_Item_ID.Value), CInt(Fields!O1_Parent_Line_Item_ID.Value), Fields!O1_Parent_Line_Item_ID.Value))
Use a SWITCH statement instead of nested IIFs and test for non numeric first. SWITCH statements stop at the first expression that returns true.
=SWITCH (
ISNUMERIC((Fields!O1_Parent_Line_Item_ID.Value), CInt(Fields!O1_Parent_Line_Item_ID.Value),
Fields!O1_Parent_Line_Item_ID.Value = "0", ""
True, Fields!O1_Parent_Line_Item_ID.Value
)
The above statement will test each expression in turn until it finds one that returns true. So if it's numeric, convert to int. If it's "0" return and empty string. If both those return false then the last expression will always return true so you will get you original value.
Note that 0 numeric values will return 0 not "" as they are obviously numeric so the first expression will catch it.
I'm working with an ItemNumber field in a legacy system that is 99% numbers, but there are a few records that contain letters. The numbers are all padded with leading zeros so I thought I would just cast them as bigint's to solve this problem, but of course it throws an error when it gets to the records with letters in them.
I thought the following case statement would have worked, but it still throws the error. Why in the world is SQL Server evaluating the cast if the isnumeric(itemnumber) = 1 condition isn't true?
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint)
else itemnumber
end ItemNumber
from items
And what's the best workaround?
Your expression tries to convert a VARCHAR value into a BIGINT if it's numeric and leave the value as is if it's not.
Since you are mixing datatypes in the CASE statement, SQL Server tries to cast them all into BIGINT but fails on non-numeric values.
If you just want to omit non-numeric values, get rid of the ELSE clause:
SELECT CASE ISNUMERIC(itemnumber)
WHEN 1 THEN
CAST(itemnumber AS BIGINT)
END
FROM items
Maybe because:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see money and smallmoney (Transact-SQL).
http://msdn.microsoft.com/en-us/library/ms186272.aspx
The problem is that you are still mixing your types:
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint) --bigint
else itemnumber --varchar or whatever
end ItemNumber --????
from items
You need two columns
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint) --bigint
else -1
end NumericItemNumber
from items
select case when isnumeric(itemnumber) = 1
then ''
else itemnumber
end StringItemNumber
from items
Then you need to build a query that takes both ints and varchars
I have a query that looks at duty and vat information and does calculation based on the returned value.
The column that tells me the duty rates is in the table formatted as either, for example 3.7% or 8% in both bases I need remove the % from my return value. Otherwise my SUM clasue fails.
I have sorted the problem for the 3.7% example with the follwoing:
CASE WHEN CustomsTariff.CommodityCode.StandardDuty = 'Free' THEN '0.0' ELSE SUBSTRING(CustomsTariff.CommodityCode.StandardDuty, 1, 3) END AS DutyRate,
This drops the % for any returns where there is decimal palce but I need to add to the CASE to say if the StandardDuty value has no decimal places drop the % character as well without messing up the first statement that looks to the 1st 3 digits.
Thanks.
Did you try a replace() on the % character? Replace
CASE WHEN CustomsTariff.CommodityCode.StandardDuty = 'Free'
THEN '0.0' ELSE REPLACE(CustomsTariff.CommodityCode.StandardDuty, N'%', N'')
END AS DutyRate,
Hey I was wondering you know how its possible to use "-" to subtract in the SELECT part of a query. So can you also use "+" to add? I've tried that and instead of adding the values together it does this 123+0.28=1230.28 does that maybe have anything to do with the number being in text format? But I hadn't ever changed format from when i used "-" and that worked . Thanks
my code :
INSERT INTO Table( Question, Calculation)
SELECT DISTINCT 'Addition' AS Question,(T2.Calculation + T1.Calculation) AS Calculation
FROM Some_Table T2, Some_Table T1
ORDER BY T2.Question;
It might be interpreting + as string concatenation between a and b. Try "(a - 0) + (b - 0)" to force interpretation as numbers.
If T2.Calculation and T1.Calculation are text data type, use the Val() function to transform them to numbers before addition.
(Val(T2.Calculation) + Val(T1.Calculation)) AS Calculation
Edit:
When you use the minus operator with two text values (as in "2" - "1"), Access will transform the text values to their numerical equivalents, if possible. However, if either of the text values doesn't represent a valid number, the minus operator will give you a "Type mismatch" error ... as in "2" - "hans"
The plus operator works differently --- with two text values, it will attempt to concatenate them, same as if you'd used the concatenation operator (&) instead of the addition operator (+) ... "2" + "1" will give you "21" as a text value rather than the number 3. So, in that specific case, "2" + "1" is equivalent to "2" & "1".
An important distinction between the addition and concatenation operators is when one of the values is Null. "2" + Null yields Null. But "2" & Null yields "2".
yes, you can use '+' to add two numbers together.
SELECT table1.Field1, table1.Field2, Field1+field2 As SumOfFields
FROM table1;
Field1 Field2 SumOfFields
1 2 3
2 3 5
EDIT:
If you have strings that you want to add together then you need to convert the fields to a number: - since it was pointed out that CLng wouldn't help the OP. It have been changed to CDbl to allow for the decimal.
SELECT table1.Field1, table1.Field2, CDbl(Field1)+CDbl(field2) As SumOfFields
FROM table1;
Just precede your formula with 0+ and it will know you're talking in numbers instead of strings:
Instead of [A]+[B]+[C] put 0+[A]+[B]+[C]