This UPDATE fails with the error "Truncated incorrect DOUBLE value: '-'" and I cannot figure out why.
UPDATE psych
SET pdqp_adm=CAST((CAST(pdqt_adm AS SIGNED)) - (CAST(pdqf_adm AS SIGNED)) AS CHAR)
WHERE pdqt_adm>0
AND pdqt_adm IS NOT NULL
AND pdqf_adm>0
AND pdqf_adm IS NOT NULL
AND pdqt_adm>=pdqf_adm
All of the columns used here (pdqp_adm, pdqt_adm, pdqf_adm) are VARCHAR(6). I can do this query and the calculation works just fine:
SELECT CAST((CAST(pdqt_adm AS SIGNED)) - (CAST(pdqf_adm AS SIGNED)) AS CHAR)
FROM psych
WHERE pdqt_adm>0
AND pdqt_adm IS NOT NULL
AND pdqf_adm>0
AND pdqf_adm IS NOT NULL
AND pdqt_adm>=pdqf_adm
Ok, this error had nothing to do with the calculated values.
When I run this, I get this same error (only for record with ID 4972):
SELECT p.id, p.pdqt_adm, p.pdqf_adm
FROM psych p
WHERE p.pdqt_adm>0
AND p.pdqt_adm IS NOT NULL
AND p.pdqf_adm>0
AND p.pdqf_adm IS NOT NULL
AND p.id=4972
As it turns out, the 2 columns being used to compare to 0 and to each other both contain the value "-". Now, why this does not affect my SELECT in my question above, but does affect my UPDATE...I have no idea.
The update and select do not necessarily process the table in the same order. You can try:
UPDATE psych
SET pdqp_adm=CAST((CAST(pdqt_adm AS SIGNED)) - (CAST(pdqf_adm AS SIGNED)) AS CHAR)
WHERE (case when pdqt_adm <> '-' then pdqt_adm else 0 end) > 0 AND
pdqt_adm IS NOT NULL AND
(case when pdqf_adm <> '-' then pdqf_adm else 0 end) > 0 AND
pdqf_adm IS NOT NULL AND
(case when pdqt_adm <> '-' then pdqt_adm else 0 end) >= (case when pdqf_adm <> '-' then pdqf_adm else 0 end);
Related
How I can add some text on result of MySQL Case Operator?
I would like to get some result like this:
I try this but get a error syntax:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN ''
ELSE ''
END) AS job_url
FROM job
Probably you want to concatenate some strings? Then use the following query, where CONCAT is added to do the concatenation:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job
You should string concatenate the href string together.
SELECT (CASE WHEN job_url_outside IS NULL THEN '' ELSE '' END) AS job_url FROM job
Try use:
CONCAT(column,'some text',column)
More information here
In your case it will be like this:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job;
DEMO here
I have a view which has a column which is calculated from other columns. For example, in my original table I have the columns A and B from which I calculate the value of column X in my view. I need the value of X in another column I have in the view - Z. But when I put it in the sub-query of my view I get an Unknown column 'X' in 'field list' SQL statement.
The sub-query of the view looks like this:
(CASE
WHEN (`users`.`A` REGEXP '^(regexone)') THEN 'ValueA'
WHEN (`users`.`B` REGEXP '^(regextwo)') THEN 'ValueB'
ELSE ''
END) AS `X`,
(CASE
WHEN
(`X` = 'ValueA')
THEN
(`users`.`C`*0.95-0.3)
ELSE ''
END) AS 'Z'
What's the correct syntax of using a computed view field in the computation of another field?
if you can you user-defined variables use like
SELECT
#x := (CASE
WHEN (`users`.`A` REGEXP '^(regexone)') THEN 'ValueA'
WHEN (`users`.`B` REGEXP '^(regextwo)') THEN 'ValueB'
ELSE ''
END) AS `X`,
(CASE
WHEN
(#x = 'ValueA')
THEN
(`users`.`C`*0.95-0.3)
ELSE ''
END) AS 'Z'
I am using a mysql case for some calculation and my demand is that if the output is negative then i will show 0 in the place of the negative column.Otherwise the positive value.
Let me post the query:
select
concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant,
PL.csm,
(CASE
WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) - (case when weekday(PL.dateEnd) = 6 then 1 else 0 end) - (case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END) Value
Now if the value is negative, then it should show 0 else will show the original value
The simplest way is simply to use greatest():
select greatest(<expression>, 0) as col
It is unclear what column/expression you want to do this for in your query. But, you can just plug it in.
The advantage of this method over other methods is:
The only needs to appear once in the query. Not having to duplicate code reduces the likelihood of errors.
There is no need for a subquery. This is an issue in MySQL only because MySQL materializes subqueries.
Simply use SIGN() in mysql
It returns 1 as a positive value and -1 is a negative value
Example
SELECT SIGN(500); -- (Output is 1)
SELECT SIGN(2500); -- (Output is 1)
SELECT SIGN(-999); -- (Output is -1)
SELECT SIGN(-5); -- (Output is -1)
please check this, haven't tested yet.
Note: this might not be the best solution
select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm,
if(
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
< 0,0,
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
) Value
Just do like this :
select temp.* , --all your columns
if(temp.value<0,0,temp.value) as Value
from
( your query mentioned above) temp;
When either one of the field is NULL, I want the returned value to be NULL as well. I have also tried reversing the logic: is not null. Still the same results.
MySQL code:
(case
when
((`creative_stg_sample_tracking_raw`.`total_samples_received` is not null)
and (`creative_stg_sample_tracking_raw`.`total_samples_forecasted` is not null))
then
(cast(`creative_stg_sample_tracking_raw`.`total_samples_received`
as signed) - cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted`
as signed))
else NULL
end) AS `received_forecasted_dif`
Screenshot:
Your code should be working, but you don't need the case. Whenever one of the values is NULL, the expression should be NULL:
(cast(`creative_stg_sample_tracking_raw`.`total_samples_received` as signed) -
cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted` as signed))
) AS `received_forecasted_dif`
I wonder if your problem is that the value is actually 'NULL' rather than NULL. That is, a string value rather than a real NULL. MySQL will treat the string as 0 in the arithmetic.
You can fix this by doing:
(case when `creative_stg_sample_tracking_raw`.`total_samples_received` <> 'NULL' and
`creative_stg_sample_tracking_raw`.`total_samples_forecasted` <> 'NULL'
then (cast(`creative_stg_sample_tracking_raw`.`total_samples_received` as signed) -
cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted` as signed))
)
else NULL
end) AS `received_forecasted_dif`
Hi I'm sure this is a simple fix but I cannot figure it out. I'm trying to label records that are overdue the completion date (CompleteDate-CurrentDate) (these numbers will be negatives) to "Overdue" for a report. I would also like the records to not be altered for the numbers that are not negatives. Here is a snippet of the code which is currently giving me NULL entries
Select CASE DATEDIFF(targetcompletedate, NOW())
When count(*) <=0 then 'Overdue'
END 'Days Left',
Any help would be greatly appreciated
Thanks
There are two variants of CASE:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result_else
END
CASE scalar_expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE result_else
END
In your case, it should be the first syntax, because you are not comparing to specific values but to a range. Instead, your query is actually using the second syntax. The count(*)<=0 expression is evaluated to a boolean which is then implicitly converted to an integer, the type implied by the DATEDIFF result.
You just need to use the first syntax, something like this:
select case when targetcompletedate is null
then 'Not set'
when DATEDIFF(targetcompletedate, NOW()) <= 0
then 'Overdue'
else DATEDIFF(targetcompletedate, NOW())
end as 'Days Left'
I would suggest that you eliminate the datediff() entirely:
Select (CASE when targetcompletedate <= NOW() the 'Overdue' else 'Days Left' end)
If you want to show things as numbers, then you want the datediff(). For clarity, I would explicitly convert to character strings:
select (case when targetcompletedate <= NOW() then 'Overdue'
else cast(DATEDIFF(targetcompletedate, NOW()) as varchar(255))
end)
Or, perhaps:
select (case when targetcompletedate <= NOW() then 'Overdue'
else concat(DATEDIFF(targetcompletedate, NOW()), ' days left')
end)
The philosophy being: don't use a function if there is a simpler and clearer way to express what you want.
However, I wonder if you want to count the number in each group:
select sum(case when targetcompletedate <= NOW() then 1 else 0 end) as NumOverdue,
sum(case when targetcompletedate <= NOW() then 0 else 1 end) as NumWithDaysLeft
Just try this code:
SELECT
CASE
WHEN datediff(dd,targetcompletedate,now()) <= 0 THEN 'Overdue'
WHEN datediff(dd,targetcompletedate,now()) > 0 THEN 'Days_left'
ELSE NULL END,
datediff(dd,targetcompletedate,now()) AS 'days'
FROM tablename
The output is like:
Overdue -23
Days_left 13