There is a column abc in a table xyz, whose datatype is int and the default value is null. We assume that a particular row exists in the table in which the value of the field abc is null.
I wish to change abc to 'its previous value + some other value' by running a query multiple times. The issue comes when updating the column first time since its value is null.
Adding null to anything returns null, so this doesn't work:
$this->db->set('abc', $value);
I tried using this instead:
$this->db->set('abc', "IFNULL(abc, 0) + $value");
But this produces:
UPDATExyzSETabc= 'IFNULL(abc, 0) + value'
But the required query is :
UPDATExyzSETabc= IFNULL(abc, 0) + value
(without the inverted commas for the value to be set)
Can this be achieved?
This doesn't work either:
$this->db->set('abc = IFNULL(abc, 0) + $value');
You may try to add the third parameter to FALSe (unescaped value)
$this->db->set('abc', 'IFNULL(abc, 0) + $value', FALSE);
or maybe
$this->db->set('abc', 'IFNULL(abc, 0) + '.$value.'', FALSE);
Related
I have a SQL Script where I am using wildcard in If Null statement.
I am looking to show all other records regardless of field has null values in it.
SELECT * FROM ABC WHERE (Value=1 AND
Prefix like IFNULL(${parameter:X},'%')
AND
Base like IFNULL(${parameter:Y},'%')
AND
Suffix like IFNULL(${parameter:Z},'%')
AND
Area like IFNULL(${parameter:Ax},'%')
);
I am expecting the result should show all values regardless if Area filed value is null
Don't use like. It will filter out NULL values in the data. Just do:
WHERE (NewestVersion = 1 AND
(Prefix like ${param:PrefixLookup} or ${param:PrefixLookup} is null) AND
(Base like ${param:BaseLookup} or ${param:BaseLookup} is null) AND
(Suffix like ${param:SuffixLookup} or ${param:SuffixLookup} is null) AND
(Custom2 like ${param:DRELookup} or ${param:DRELookup} is null)
If you have empty strings, then comparisons to NULL will not help. Just use:
WHERE (NewestVersion = 1 AND
(Prefix like ${param:PrefixLookup} or ${param:PrefixLookup} = '') AND
(Base like ${param:BaseLookup} or ${param:BaseLookup} = '') AND
(Suffix like ${param:SuffixLookup} or ${param:SuffixLookup} = '') AND
(Custom2 like ${param:DRELookup} or ${param:DRELookup} = '')
I have a SQL query which already uses an IF() function to check some conditions on basis of which respective expression is returned. I have to add another condition (which will be another 'and') inside the IF() which will affect the attributes from both the expressions. The query is,
select
#{booking_variable}.travel_id as operator_id,
round(sum(#{ticket_variable}.customer_commission), 2) as commission,
IF(
(#{booking_variable}.is_online = #{AdminType::YES}
and #{booking_variable}.is_ts_payment = #{AdminType::YES}),
round(sum(-#{ticket_variable}.customer_commission
- #{ticket_variable}.convenience_charge_amount
+ #{ticket_variable}.our_convenience_charge_amount), 2),
round(sum(#{ticket_variable}.adult_fare
- #{ticket_variable}.customer_commission
+ #{ticket_variable}.service_tax_amount
+ #{ticket_variable}.our_convenience_charge_amount
- #{ticket_variable}.offer_discount), 2)
) as amount_to_be_paid,
round(sum(#{booking_variable}.total_fare)) as total_fare,
0 as cancel_fare,
round(sum(#{ticket_variable}.adult_fare
+ #{ticket_variable}.service_tax_amount
- #{ticket_variable}.offer_discount )) as net_amount,
#{booking_variable}.travel_name as operator_name,
ROUND(sum(#{ticket_variable}.service_tax_amount), 2) as total_service_tax,
ROUND(sum(#{ticket_variable}.convenience_charge_amount), 2) as total_convenience_charge_amount,
ROUND(sum(#{ticket_variable}.our_convenience_charge_amount), 2) as total_our_convenience_charge_amount,
0 as cancelled_convenience_charge_amount,
(select users.branch_id from users where id=#{ticket_variable}.booked_by) as travel_branch_id
from #{booking_variable} use index(index_#{booking_variable}_travel_date),
#{ticket_variable}
where #{conditions1[0]}
group by travel_branch_id, operator_id
Here, inside the IF() expression, I have to check another variable's value . If the value is true, then the query should run as is, if not, the value of #{ticket_variable}.convenience_charge_amount + #{ticket_variable}.our_convenience_charge_amount and #{ticket_variable}.our_convenience_charge_amount is to be taken as 0.
Any help appreciated.
You can nest IF() expressions.
If you need part of your calculation to be conditional, you can use an IF() for that.
...
IF(
(#{booking_variable}.is_online = #{AdminType::YES}
and #{booking_variable}.is_ts_payment = #{AdminType::YES}),
round(sum(-#{ticket_variable}.customer_commission
- IF((...some other condition...),
#{ticket_variable}.convenience_charge_amount
+ #{ticket_variable}.our_convenience_charge_amount,
0)
), 2),
...
I'm expecting only the names to appear on the calendar if they exist and nothing if they don't.
I created an expression where it returns only the last name of a person by using the comma as a delimiter.
My current expression:
=iif(IsNothing(Fields!EmployeeName.Value), nothing, Left(Fields!EmployeeName.Value,-1 + InStr(Fields!EmployeeName.Value, ",")))
Current results where #Error appears if a name doesn't exist:
The error is occuring because you are passing in a number less than 0 to the left function. When your string does not have a comma in it you are passing in -1.
To handle this I added two if statements to the expression. The first will return the whole string if the index of the first comma is 0. The second checks for the -1 condition and passes a 0 to the left function when that occurs.
=
iif(
IsNothing(Fields!EmployeeName.Value),
nothing,
iif(
InStr(Fields!EmployeeName.Value, ",") = 0,
Fields!EmployeeName.Value,
Left(Fields!EmployeeName.Value,iif(-1 + InStr(Fields!EmployeeName.Value, ",") < 0, 0, -1 + InStr(Fields!EmployeeName.Value, ",")))
)
)
I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)
i think this one is simple but i cant find a good example.
What i have is three columns. StartPrice EndPrice Dividens and results. Each of these is a decimal(10,2) column in MySql. What i'm looking for is (preferably) is an update statement that basically will do Dividens / (AddPrice + EndPrice) and return the decimals.
Example:
Dividens (AddPrice + Endprice) = result
2.09 / (7.52 + 10.52) = .11
7.40 / (1.75 + 1.25) = 2.47
All columns are decimal(10,2)
I'm looking for basically
update tblmath
set result = '<this is what i cant do>
where result = null
Assuming your table is called tblmath and you're trying to update a column called result to contain the result of the calculation, this should work for you:
update tblmath set result = Dividens / (AddPrice + Endprice) where result is null;
BTW: You misspelled "Dividends", but I preserved the misspelling in the SQL above.