What am I doing wrong, I keep getting errors about syntax.
UPDATE common_lookup
SET common_lookup_column =
CASE
WHEN common_lookup_table = 'MEMBER' THEN
CASE
WHEN common_lookup_type IN ('INDIVIDUAL', 'GROUP') THEN
common_lookup_table || '_TYPE'
WHEN common_lookup_type LIKE '%CARD' THEN
'CREDIT_CARD_TYPE'
END
ELSE
common_lookup_table || '_TYPE'
END;
In MySQL, using the concat() function:
UPDATE common_lookup
SET common_lookup_column = (CASE WHEN common_lookup_table = 'MEMBER'
THEN (CASE WHEN common_lookup_type IN ('INDIVIDUAL', 'GROUP')
THEN concat(common_lookup_table, '_TYPE')
WHEN common_lookup_type LIKE '%CARD'
THEN 'CREDIT_CARD_TYPE'
END)
ELSE concat(common_lookup_table, '_TYPE')
END);
Assuming that you don't intend to have NULL values from the inner case, you can simplify this logic to:
UPDATE common_lookup
SET common_lookup_column = (CASE WHEN common_lookup_table = 'MEMBER' AND
common_lookup_type LIKE '%CARD'
THEN 'CREDIT_CARD_TYPE'
ELSE concat(common_lookup_table, '_TYPE')
END);
The operation || is the string concatenation operator in several databases.
Related
I got 2 CASE statements: First CASE statement is as follows:
Case When ((cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '')
AND (comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')) Then 'Pending'
ELSE 'Shipped' End AS shipment_status
The Second CASE statement is as follows:
Case When apbg_bal_pay_amt ='0' Then 'Received'
Else 'Pending' End AS payment_status
Iam looking to write one more CASE statement named OVERALL_Status. That is basically a combination of both this CASES (shipment_status and payment_status), which means if Shipment status is 'Shipped' AND Payment_Status is 'Received' then Overall_status is 'Completed' else 'Not Completed'. Can anyone please help me on this. I am really struck here. I tried the combination of both the CASES, but not working:
Case When (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') AND
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '') AND (apbg_bal_pay_amt != '0') Then
'Pending' ELSE 'Completed' End AS overall_status
There is two solutions as I see it:
1. You make the current query a subquery and add another CASE statement on the result of the subquery. (the pretty solution as I see it)
2. You add another WHEN to the existing case. Combining the clauses in the first two WHEN clauses to get the result. (probably the most optimized solution)
How about this?
(case when (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') and
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')
then 'Not Completed'
when apbg_bal_pay_amt = '0' Then 'Completed'
else 'Not Completed'
end) as overall_status
How should I make if condition in mysql where time = null then I want result in NO else YES?
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) != NULL THEN
#Loss:='YES'
ELSE
#Loss:='NO'
END AS Loss
from Event01,(SELECT #Loss:= 0) AS Loss
Query
SET #Loss := 0;
SELECT
#Loss:= case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NOT NULL
THEN 'YES'
ELSE 'NO' END
FROM Event01;
Try this, You have to check NULL using is clause.
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) is not NULL THEN
'YES'
ELSE
'NO'
END AS Loss
from Event01
Hope it will help.
Try this
SELECT case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NULL THEN 'NO' ELSE 'YES' END AS Loss from Event01
I would like to use if statement in sql query :
what I want :
if(tractions_delivery.send_date_id !=0 ){
date_send_commodities.id = tractions_delivery.send_date_id
}
my query :
from
tractions_delivery,user_address,province,city,date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id
AND
tractions_delivery.address_id = user_address.id
AND
user_address.province_id = province.id
AND
user_address.city_id = city.id
AND
//not work
(tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id = tractions_delivery.send_date_id)
AND
users.id = user_address.user_id
You could use the CASE-statement
SELECT
*
FROM
tractions_delivery,
user_address,
province,
city,
date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id AND
tractions_delivery.address_id = user_address.id AND
user_address.province_id = province.id AND
user_address.city_id = city.id AND
CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
users.id = user_address.user_id
You can only use if statements in stored procedures or functions. If you just write a sql statement unfortunately you cannot use if statements around the query. But you can use logic in the query itself, e.g.:
SELECT CASE WHEN col1 = col2 THEN'col1 equals col2' else 'col1 doesnt equal col2' ELSE
FROM table1
So around doesnt work, but in the field list you can create CASE WHEN ELSE END logic.
CASE or IF() operators can be of help.
Examples,
SELECT (CASE 1 WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE 'More' END) 'Result';
OR
SELECT IF(1=1, 'One', 'Two') 'Result';
These CASE and IF() operators can be used in the SELECT clause to conditionally interpret column values and return in the resultset.
Note: Do not confuse CASE operator here with 'CASE conditional syntax block' that ends with END CASE.
I'm having issues with writing a case statement in SQL. My first question is: Is it possible to write the if statement below as a case statement in a SQL Query within the select statement?
If no, then please have a look at the case statement below and help/guide me to get into a valid format. Thanks and much appreciated!
IF (var1 = 1){
do this1;
IF (var 1 = 2){
Do this2;
}Else{
do something else1;
}
Else if (Var 1 = 3){
Do this3;
}Else{
Do something else2;
}
Here is my case statement. I know it doesn't work because it's not a valid case statement. Could someone kindly help me in making it a valid case statement. Thanks in advance.
SELECT
CASE
WHEN apple.type = 1 OR apple.type = 2
THEN basket.S1
ELSE
CASE
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
ELSE basket.S2
ELSE
CASE
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
END
END
FROM .....
WHERE .....
I think you are over complicating your case statement , looking at your first example you case statement should be fairly simple,
something like ....
SELECT CASE
WHEN #Var1 = 1 THEN 'Something 1'
WHEN #Var1 = 2 THEN 'Something 2'
WHEN #Var1 = 3 THEN 'Something 3'
ELSE 'Something Else'
END
FROM .....
WHERE .....
You case statement can we written something like this...
SELECT
CASE
WHEN apple.[type] IN (1,2)
THEN basket.S1
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
Since you're checking for nulls and substituting non-null values, you can make the query shorter by using the COALESCE function.
SELECT
CASE
WHEN apple.type IN (1, 2) THEN basket.s1
WHEN apple.type = 0 THEN COALESCE(basket.s2, basket.s1)
WHEN apple.type = 3 THEN COALESCE(basket.s3, basket.s1)
END
FROM ...
I want to update multiple columns using case statement, I achieved this but it is not a best way i have to do same task three times, how can i achieve in one statement here is my test sql script:
Delimiter //
create procedure test_f()
begin
update test set
#### total####
test.total = case when test.currencyid='INR' then test.total/85.09
Else test.total End,
test.total = case when test.currencyid='SEK' then test.total/8.97
Else test.total End,
### Commission ####
test.commission = case when test.currencyid='INR' then test.commission/85.09
Else test.commission End,
test.commission = case when test.currencyid='SEK' then test.commission/8.97
Else test.commission End,
test.currencyid = case when test.currencyid in ('SEK','INR') then 'EUR'
Else test.currencyid End
WHERE test.currencyid in ('SEK','INR') ;
END //
Now i want to update all three columns altogether based on currencyid.
You could try this for the first case:
instead of:
update test set
#### total####
test.total = case when test.currencyid='INR' then test.total/85.09
Else test.total End,
test.total = case when test.currencyid='SEK' then test.total/8.97
Else test.total End,
change to:
update test set
#### total####
test.total =
case when test.currencyid='INR' then test.total/85.09
when test.currencyid='SEK' then test.total/8.97
Else test.total
End,
do the same for ### Commission ####
I would be inclined to write the query as:
update test t
set t.total = t.total / (case when t.currencyid = 'INR' then 85.09 else 8.97 end),
t.commission = t.commission / (case when t.currencyid = 'INR' then 85.09 else 8.97 end),
t.currency = 'EUR'
where t.currencyid in ('INR', 'SEK') ;
The where clause limits the currencies to the two mentioned, so the case only has to test for those two conditions. Simplifying the case statement also makes it clearer that the same logic is being used for both calculations.