After looking at similar issues I'm no wiser. What I have is a value returning that has two sets of numbers then a name i.e. (xxxx;xxxx;name). I'm trying to just return the name. The original code I've written (below) works if a name value/name is present.
SELECT
SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE))
FROM
[RWADMIN].[RV_ACTIVITY_FIELDS] P
JOIN
[RWADMIN].[RW_ASSOCIATION] A ON P.activity_ID = A.activityA_id
However it breaks the report if the value is blank as it returns a "NULL",
So I've thought this would work, but I get the above error.
SELECT
ISNULL(SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE)))
FROM
[RWADMIN].[RV_ACTIVITY_FIELDS] P
JOIN
[RWADMIN].[RW_ASSOCIATION] A ON P.activity_ID = A.activityA_id
Help please.
IsNull takes an expression and a value to replace the null values. It check the expression value, if it is null returns the value that we provided for nulls and if not just returns the expression value, so if you want to return a blank when it is null you should use it this way:
ISNULL(SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE)), '')
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 have a specific column in a table, it shall contains only numbers in Nvarchar that have a length of 3. Unfortunately, some users wrote '12' but they should have written '012'. There were not enough validation at the time.
I need to fix that. Here is the logic I used :
UPDATE [Mandats_Approvisionnement].[dbo].[ARTICLE_ECOLE]
SET [UNIT_ADM] = STUFF(UNIT_ADM, 0, 0, '0')
WHERE LEN(UNIT_ADM) = 2;
The error goes like :
Cannot insert the value NULL into column 'UNIT_ADM', table
'Mandats_Approvisionnement.dbo.ARTICLE_ECOLE'; column does not allow
nulls. UPDATE fails.
I can't see where the problem is, I verified and all the records contain at least 2 characters, so the STUFF function cannot returns null as there are no NULL records in that table column [unit_adm]... How do I make it work ?
It should be stuff(UNIT_ADM,1,0,'0') as stuff returns null if the start position is 0.
Citing the documentation:
If the start position or the length is negative, or if the starting
position is larger than length of the first string, a null string is
returned. If the start position is 0, a null value is returned.
You could make this simpler by using
right('0' + UNIT_ADM, 3)
instead of stuff.
If I give decode(1,2,3,4,5) it just display null value I know it is because no expression in matched with search value so default value is returned.But I want a explanation about how it is matched.And also if I give same function decode(a,b,c,d,e) it returns invalid identifier e,why?
I suspect you just need to put your character data into quotes:
select decode('a','b','c','d','e') from dual
But you asked for an explanation of how it is matched so here from the Oracle docs on DECODE
If expr and search are character data, then Oracle compares them using nonpadded comparison semantics. expr, search, and result can be any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2. The string returned is of VARCHAR2 datatype and is in the same character set as the first result parameter.
If the first search-result pair are numeric, then Oracle compares all search-result expressions and the first expr to determine the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.
EDIT: "I want to know the reason why it specifically says e is invalid integer and not the other characters."
It is returning the error on the last result expression, not just the e. So if you stop at c or go to g you get whatever is last. My best guess is it just reporting the last error..possibly because of the way it the engine is parsing, Might find the "last" error first and report it.
In fact you see the same thing with
select a,b,c from dual
so nothing to do with decode.
From your comments Senthil, clearly it is the mechanics of DECODE that you are unclear on. I will try to explain.
The first element in the list is the value that you want to match. After that, we deal in pairs of elements where if you match to the first item in the pair, the returned value from decode is the second item in the pair. You can have as many pairs to match to as you want. And finally you can optionally add a default return value if no match is found.
So DECODE(a,b,c,d,e) means
Evaluate a.
If a = b, return c
If a = d, return e.
So DECODE(1,2,3,4,5) is saying
Evaluate 1
If 1=2, return 3
If 1=4, return 5
Since 1 is not equal to 2 or 4, you get a null return.
If you added a final default return value, you would get that
DECODE(1,2,3,4,5,6)
Evaluate 1
If 1=2, return 3
If 1=4, return 5
If no matches found, return 6.
This is why I tend to format my calls to DECODE to clearly show the pairs. I would write it as:
Select
DECODE( field_x
,matchvalue1, return1
,matchvalue2, return2
...
,matchvalueN, returnN
<,default_Value_if_appropriate>)
...
I am selecting values from the database using Functions. I have three different functions in which if the first does not return anything I check for the second and third functions for values using NVL2. The problem is when I execute independent functions they return values but not in the NVL2 function My query is:
select NVL2(CRBP.FPDATE(LAM.ACID),
CRBP.FPDATE1(LAM.ACID),
CRBP.ODSANCDATE(GAM.ACID)) from
tbaadm.lam, TBAADM.GAM
where gam.acid = lam.acid
and gam.acid = 'VM12990'
This does not return any value.
But when I execute :
select CRBP.FPDATE (LAM.ACID) from tbaadm.lam where lam.acid = 'VM12990';
This Already returns a value. Shouldn't this be the value returned by my first query considering that it is the first in the check. What is the Problem with My first Query??
If CRBP.FPDATE returns a value the query should return the value returned by the function CRBP.FPDATE1 and if CRBP.FPDATE returns NULL then the query should return CRBP.ODSANCDATE's vlaue.
Does CRBP.FPDATE1 return anything?
NVL2(a,b,c) is a "if a is null then a else b" type function. so the value of a is never returned.
it sounds like you want COALESCE which will return the first not null value from the set of parameters
I have below query in Ms-Access but I want to replace Blank value with zero but I can't get proper answer. Is there any way to replace blank value in zero.
(SELECT
SUM(IIF(Review.TotalPrincipalPayments,0,Review.TotalPrincipalPayments))+
SUM(IIF(Review.TotalInterestPayments,0,Review.TotalInterestPayments ))
FROM
tblReviewScalars as Review
INNER JOIN tblReportVectors AS Report ON(Review.LoanID=Report.LoanID)
WHERE Report.AP_Indicator="A" AND Report.CashFlowDate=#6/5/2011# AND Review.AsofDate=#6/5/2011# AND ( Review.CreditRating =ReviewMain.CreditRating)) AS [Cash Collected During the Period],
I assume TotalPrincipalPayments and TotalInterestPayments are both numeric types, hence the 'blanks' in question is the NULL value.
In SQL, the set function SUM will disregard NULL values, unless all values resolve to NULL in which case NULL is returned (erroneously and the error is with SQL not Access for a change :)
To use a simple example, SELECT SUM(a) FROM T; will only return NULL when a IS NULL is TRUE for all rows of T or when T is empty. Therefore, you can move the 'replace NULL with zero' logic outside of the SUM() function. Noting that "NULLs propagate" in calculations, you will need to handle NULL for each SUM().
You haven't posted the whole of your query e.g. the source of the correlation name ('table alias') ReviewMain is not showm. But it seems clear you are constructing a derived table named "Cash Collected During the Period", in which case your calculated column needs an AS clause ('column alias') such as TotalPayments e.g.
...
(
SELECT IIF(SUM(Review.TotalPrincipalPayments) IS NULL, 0, SUM(Review.TotalPrincipalPayments))
+ IIF(SUM(Review.TotalInterestPayments) IS NULL, 0, SUM(Review.TotalInterestPayments))
AS TotalPayments
FROM tblReviewScalars as Review
INNER JOIN tblReportVectors AS Report
ON Review.LoanID = Report.LoanID
WHERE Report.AP_Indicator = 'A'
AND Report.CashFlowDate = #2011-05-06#
AND Review.AsofDate = #2011-05-06#
AND Review.CreditRating = ReviewMain.CreditRating
) AS [Cash Collected During the Period], ...
An alternative to #onedaywhen's answer is to use the nz function, which is specifically for null-substitution:
SELECT
SUM(NZ(Review.TotalPrincipalPayments,0))+
SUM(NZ(Review.TotalInterestPayments,0))
...
As onedaywhen pointed out, this is functionally equivalent to putting the function outside the aggregate, which may perform better (the function is called once, rather than once per un-aggregated row):
SELECT
NZ(SUM(Review.TotalPrincipalPayments),0)+
NZ(SUM(Review.TotalInterestPayments),0)
...
To change a null value to a zero in an Access 2010 database, open your table, go to design view, click on the field and set the default value to: =0.