I tried to run the script below, and it will display NULL value. I only want to get data for ALBANIA without having NULL value.
SELECT [COUNTRY_CODE_NEW]
,CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' THEN 'ALBANIA'
END 'COUNTRY'
FROM [dbo].[COUNTRY_NEW]
Can you do:
CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' AND IS NOT NULL THEN 'ALBANIA'
Just add a check to ensure it's not null:
SELECT
[COUNTRY_CODE_NEW],
CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' AND [COUNTRY_CODE_NEW] IS NOT NULL THEN 'ALBANIA'
END 'COUNTRY'
FROM [dbo].[COUNTRY_NEW]
Related
I am working in a company where we use Spring -Hibernate and mysql database at backend.
There is a table tc_wallet
In this table i have an column tips_type which has values
Collection,Payable
'' (empty)
NULL --> No value has been initialized
Now when i fire a query:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND `tips_type` != 'Collection'
I get results which has column value as '' (empty).
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
But even (NULL) is != 'Collection' should satisfy the above condition.
So according to me the result should have been .
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar (NULL)
gaurav.wakharkar (NULL)
Is there some issue while checking/comparing values with (NULL) ?
Does it behave differently ?
To check for nullness, you want to use IS NULL. Comparing NULL to something else with the equality operator (or the inequality operator) is always false.
Consider:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` IS NULL OR `tips_type` != 'Collection')
change your query to
SELECT
*
FROM
`tc_wallet`
WHERE login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` != 'Collection' or `tips_type` is null)
I have a table in MySQL where in a record a field (Default: None) does not have any value.
I'm using a stored procedure to select values from this table and I when this field has no value I should get the value N/A.
I tried the following code but I get the field with no value.
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(case
when md.device_and_implant_description = ''
then 'N/A'
when md.device_and_implant_description !=0
then md.device_and_implant_description
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id
The p_id value is given by the user.
This is the result:
This is the structure of my table:
Please use NUll as well as "".
If you want to use NULL or '' only then before storing data, make sure that you are storing null or empty string then accordingly use condition.
Use REGEXP condition instead of ''
SELECT md.coding_id, md.patient_id, md.implant_date,
CASE WHEN md.device_and_implant_description REGEXP '^[A-Za-z0-9]+$'
THEN md.device_and_implant_description
ELSE 'N/A'
END AS device_and_implant_description,
md.device_and_implant_description
FROM medical_devices_mapping md
WHERE md.patient_id = p_id
Maybe you should invert your logic
case when trim(md.device_and_implant_description) is not null then md.device_and_implant_description
else 'N/A'
end
I found the solution using COALESCE().
The proposed solution is the following:
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(CASE when COALESCE(md.device_and_implant_description, '') !=''
then md.device_and_implant_description
when COALESCE(md.device_and_implant_description, '') =''
then 'N/A'
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id
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
I have a mysql table as follows:
name | serialno | key
---------------------
NULL | NULL | 10
luke | 1234 | 20
NULL | NULL | 30
NULL | NULL | 40
I have to update a row where the name and the serialno update for a valid key.
The update statements that I am running are as follows:
UPDATE test SET Name = 'pc', Serialno = '10', WHERE `Key` = '10' AND name is null
and
UPDATE test SET Name = CASE
WHEN NULL THEN 'pc'
END
SET Serialno = CASE
WHEN NULL THEN '10'
END
Where `key` = '20'
The following errors get displayed:
1.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Key = '10' AND name is null' at line 1:
2.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET Serial = CASE
WHEN NULL THEN '10'
END
Where `key`='20'' at line 4:
Please help.
UPDATE test SET Name = CASE Name
WHEN NULL THEN 'pc'
END,
SET Serialno = CASE Serialno
WHEN NULL THEN '10'
END
Where `key` = '20'
You have to specify condition of your CASE
I don't really know what is the context (can you explain the detail what you want and then I will help you with better result).
However, as the StanislavL's answer, the syntax was wrong. I corrected it as below:
UPDATE test SET Name = CASE Name
WHEN NULL THEN 'pc'
END,
Serialno = CASE Serialno
WHEN NULL THEN '10'
END
Where `key` = '20'
Please do like this
UPDATE test SET Name = 'pc', Serialno = '10' WHERE `Key` = '10' AND name is null
Reasons for Errors:
Error 1 was because you have a , comma before WHERE Key = '10' ....
Statement should be:
UPDATE test SET Name = 'pc', Serialno = '10' WHERE `Key` = '10' AND name is null
Error 2 was because you have not instructed which case value to check in when clause. Acceptable syntaxes are:
1. CASE column_name WHEN value THEN expr1 ELSE expr2 END
2. CASE WHEN column_name <condition> value THEN expr1 ELSE expr2 END
Statement should be:
UPDATE test
SET
Name = CASE WHEN name IS NULL THEN 'pc' else name end
, Serialno = CASE WHEN serialno IS NULL THEN '10' else serialno end
END
Where `key` = '10'
Example # SQL Fiddle
You can extend this to set values for columns based on different key values without using where clause.
Example:
UPDATE test
SET
Name = CASE WHEN `key`=10 and name IS NULL THEN 'pc' else name end
, Serialno = CASE WHEN `key`=10 and serialno IS NULL THEN '10' else serialno end
, Name = CASE WHEN `key`=30 and name IS NULL THEN 'key is 30' else name end
, Serialno = CASE WHEN `key`=30 and serialno IS NULL THEN '3' else serialno end
END
Refer to: MySQL: CASE - WHEN syntax
I have this part of query:
IF(orders = NULL OR orders = '', "value1', 'value2')
which works with empty cells but not with null ones, any help?
When it's NULL it doesn't make anything but when it's '' it runs the query
It's spelled orders is NULL (not orders = NULL).
You have to use
IF(orders IS NULL OR orders = '', 'value1', 'value2')
instead