SugarCRM Calculated field using IfElse, needs to return null on true - sugarcrm

I have an ifElse function of this construct:
ifElse ( condition, return null, otherwise return today's date )
As things are currently my function returns itself. The field is a date called course_date:
ifElse ( condition, $course_date, today() )
and this returns 0 on condition true. This then causes an error as 0 is not a date and then fails when being submitted.
I would like to know if it is possible to return null or an empty string.

Create another datetime field called null_datetime (for example) with a default value of none.Then change the formula to display that field on true.
ifElse ( condition, $null_datetime, today() )

Related

Update with ISNULL and operation

original query looks like this :
UPDATE reponse_question_finale t1, reponse_question_finale t2 SET
t1.nb_question_repondu = (9-(ISNULL(t1.valeur_question_4)+ISNULL(t1.valeur_question_6)+ISNULL(t1.valeur_question_7)+ISNULL(t1.valeur_question_9))) WHERE t1.APPLICATION = t2.APPLICATION;
I know you cannot update 2 tables in a single query so i tried this :
UPDATE reponse_question_finale t1
SET nb_question_repondu = (9-(COALESCE(t1.valeur_question_4,'')::int+COALESCE(t1.valeur_question_6,'')::int+COALESCE(t1.valeur_question_7)::int+COALESCE(t1.valeur_question_9,'')::int))
WHERE t1.APPLICATION = t1.APPLICATION;
But this query gaves me an error : invalid input syntax for integer: ""
I saw that the Postgres equivalent to MySQL is COALESCE() so i think i'm on the good way here.
I also know you cannot add varchar to varchar so i tried to cast it to integer to do that. I'm not sure if i casted it correctly with parenthesis at the good place and regarding to error maybe i cannot cast to int with coalesce.
Last thing, i can certainly do a co-related sub-select to update my two tables but i'm a little lost at this point.
The output must be an integer matching the number of questions answered to a backup survey.
Any thoughts?
Thanks.
coalesce() returns the first non-null value from the list supplied. So, if the column value is null the expression COALESCE(t1.valeur_question_4,'') returns an empty string and that's why you get the error.
But it seems you want something completely different: you want check if the column is null (or empty) and then subtract a value if it is to count the number of non-null columns.
To return 1 if a value is not null or 0 if it isn't you can use:
(nullif(valeur_question_4, '') is null)::int
nullif returns null if the first value equals the second. The IS NULL condition returns a boolean (something that MySQL doesn't have) and that can be cast to an integer (where false will be cast to 0 and true to 1)
So the whole expression should be:
nb_question_repondu = 9 - (
(nullif(t1.valeur_question_4,'') is null)::int
+ (nullif(t1.valeur_question_6,'') is null)::int
+ (nullif(t1.valeur_question_7,'') is null)::int
+ (nullif(t1.valeur_question_9,'') is null)::int
)
Another option is to unpivot the columns and do a select on them in a sub-select:
update reponse_question_finale
set nb_question_repondu = (select count(*)
from (
values
(valeur_question_4),
(valeur_question_6),
(valeur_question_7),
(valeur_question_9)
) as t(q)
where nullif(trim(q),'') is not null);
Adding more columns to be considered is quite easy then, as you just need to add a single line to the values() clause

Crystal Reports If true then return number else return NULL

In Crystal Reports, is it possible to have a function that returns a numeric value if the if statement evaluates to true and returns NULL otherwise?
I currently have
IF ({INDICATOR} = 'Y') Then
(
{numeric value}
)
Else
(
0
);
But since 0 is a possible value of {numeric value} it doesn't make sense. Rather, I would rather have that field come up blank if the indicator isn't 'Y', but when I replace the 0 with NULL it gives me a type mismatch error.
Is there a way for me to only show the value when the indicator is 'Y'?
If you truly want a null value and not empty try the following
create a formula called NULL then save it and close without entering any data in the formula area. Then in your formula above try
If {INDICATOR} = 'Y' then {numeric value}
else tonumber({#NULL})
you can't return two different datatypes in a single if statement..If if is number then else should also be number.. instead try to split the statements and try.. something like below.
IF ({INDICATOR} = 'Y') Then
(
ToText({numeric value})
)
Else if ({INDICATOR} <> 'Y') Then
(
""
);
If {INDICATOR} = 'Y' then {numeric value}
else {Command.NULLCOL}
The setup in Database Expert is to use Add Command with sql:
select null as nullcol
from dual
Then left join to it.
A returned null value can be very powerful, so your need for a null value should not be questioned. Null values automatically display differently to stand out. Compared to 0 or "", null values work correctly with DistinctCount function. Null values also work correctly with section summaries and crosstabs, which can save you a lot of work which is the whole point of using crystal.

How to write custom function in crystal report to be used for record selection

I am confused in writing custom function in crystal report to fetch record using record selection.
When we create formula in record selection it will add where clause in the generated SQL-Query on the basis of parameters used. Now i want to write the custom formula, which will extract the record pro-grammatically :
Even i have written a function :
Function (stringVar st)(
stringVar selection;
if (st <> 'ALL') then (
selection = st;
)
else(
//In this case the user select only single value, it will fetch the result to that //particular column value in the table.. otherwise it leaves that particular row..
selection = "multiple selection";
)
)
Now the code to use the custom function in record selection using select expert would be :
if(myfunction({?parameter1}) <> "ALL") then
(
// what code should i write to select that particular record...
if(myfunction({?parameter2})) <> "ALL" ) then
(
//do selection from the previously selection of rows which have this parameter
if(myfunction({?parameter3}) <> "ALL") then
(
//do selection from the previously selection of rows which have this
//parameter
)
else (//do something else)
)
else (//do something else)
)
else (//do something else)
Thanks, in advance.
You don't need a custom function for this. Assuming that you have two string parameters, ry:
// assumes you have a parameter value of 'All Countries' w/ a value of '0'
( '0' IN {?CountryCodes} OR {table.CountryCode} IN {?CountryCodes} )
// assumes you have a parameter value of 'All Regions' w/ a value of '0'
AND ( '0' IN {?RegionCodes} OR {table.RegionCode} IN {?RegionCodes} )

How do return a Constant Value in the Sql Else Clause when only on value remain's the same

UPDATE ConcordWholesales_new.dbo.Product
SET CCCQTYINSTOCK=isnull(#newQTYINSTOCK,'')
where SKU=#newSTOCKCODE
UPDATE Product
SET Published = CASE WHEN CCCQTYINSTOCK=isnull(#newQTYINSTOCK,'')
THEN '1' ELSE 0 END
I have an Update Trigger that Update's Stock Quantity's when its below 0 the Product becomes Unpublished. If the StockQuantity goes above 0 the above Case Updates the Published feild to republish the Product.
When I update the ELSE clause returns a 0 value on the other columns, but If I remove the ELSE clause it can't insert a null value.
How do I return the above code so it only Updates one line of data.
you were missing WHERE clause in second statement
UPDATE Product
SET Published = CASE WHEN CCCQTYINSTOCK > 0
THEN 1 ELSE 0 END
where SKU=#newSTOCKCODE
and one more thing, what datatype is Published column ?
you are using '1' - its nchar, and 0 - int

SSRS Parameters. Allowing "All" or "Null"

SSRS parameters are a pain. I want to be able to re-use reports for many different needs by allowing the users access to many different parameters and making them optional.
So, if I start out with code such as:
Select * from mytable myt
where myt.date between '1/1/2010' and '12/31/2010'
and year(myt.date) = '2010'
and myt.partnumber = 'XYZ-123'
I want those parameters to be optional so my first attempts were to make the parameters default to null such as:
and (myt.partnumber = (#PartNumber) or (#PartNumber) is null)
That has problems because if the database fields in question are nullable then you will drop records because null does not equal null.
I then used code such as this:
DECLARE #BeginDate AS DATETIME
DECLARE #EndDate AS DATETIME
DECLARE #PartNumber AS VARCHAR(25)
SET #Year = '..All'
SET #BeginDate = '1/1/2005'
SET #EndDate = '12/31/2010'
SET #PartNumber = '..All'
SET #Year = '..All'
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (year(myt.date) = (#Year) or (#Year) = '..All' )
and (myt.partnumber = (#PartNumber) or (#PartNumber) = '..All')
That doesn't work because Year(myt.date) is an integer and #Year is not.
So, here are my questions.
How can I make my dates optional? Is the best way to simply default them to dates outside of a practical range so I return all values?
What is the best way to handle the null or '..All' options to make my queries as readable as possible and allow my users to have optional parameters for most data types? I'd rather not use null for
Go ahead and allow nulls, which indicates the filter should not be applied. Then, you can use the following:
SELECT *
FROM mytable myt
WHERE COALESCE(myt.date, '1/1/1900') between COALESCE(#BeginDate, myt.date, '1/1/1900') and COALESCE(#EndDate, myt.date, '1/1/1900')
AND COALESCE(YEAR(myt.date), -1) = COALESCE(#Year, YEAR(myt.date), -1)
AND COALESCE(myt.partnumber, -1) = COALESCE(#PartNumber, myt.partnumber, -1)
In summary, if any variable value is NULL, then compare the column value to itself, which effectively ignores the condition. More specifically, when testing myt.date, if #BeginDate is NULL then set the lower range value equal to the myt.date value. Do the same substitution with the #EndDate value. Even, if both #BeginDate and #EndDate are NULL, the condition will be true.
A similar approach is used for YEAR(myt.date) and myt.partnumber. If the variable value is NULL, then compare the column value to itself, which is always true.
UPDATE:
Added a default value to each COALESCE to handle the situation where the column value is NULL.
I like your third code block. It seems like your WHERE clause could be corrected to work with a non-int value. The AND clause for the year line would look like this--not my best T-SQL, but it should get you pointed in the right direction:
and 1 = CASE #Year WHEN '..All' THEN 1 ELSE CASE WHEN year ( myt.date ) = CONVERT ( int, #Year ) THEN 1 ELSE 0 END END
This will allow you to have a string value of '..All' or an int value. Either will match correctly. You can do the same with partnumber.
try it like this, the key is to fix your null parameters values to surrogate nulls, also since sql server supports short circuit evaluation, putting the null check should generally perform better.
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (#Year IS NULL OR COALESCE(myt.date,'1900') = #Year)
and (#PartNumber IS NULL OR ISNULL(myt.partnumber, '<NULL>') = (#PartNumber)