简体   繁体   中英

CASE statement in SQL Server with nested select

I have an issue in SQL Server 2017, I have a query with case statement when I execute the query it returns me some null fields, this is my query:

select
    case item_value_text
        when 'Low Profile Desktop' then 'Desktop'
        when 'Mini Tower' then 'Desktop'
        when 'All in One' then 'Desktop'
        when 'Portable' then 'Notebook'
        when 'LapTop' then 'Notebook'
        when null then 'No Informado'
        when 'Other' then 'Maquina Virtual'
        else item_value_text
    end as item_value_text
from 
    inv_generalinventory_item GIV
where
    GIV.object_uuid = DHW.dis_hw_uuid and
    item_name_id = 1 anmd 
    item_root_name_id = 1 and
    item_parent_name_id = 2 and
    (item_value_text like 'All in One' or
     item_value_text like 'Bus Expanson chassis' or
     item_value_text like 'Desktop' or
     item_value_text like 'Docking Station' or
     item_value_text like 'Expansion chassis' or
     item_value_text like 'Hand Held' or
     item_value_text like 'Laptop' or
     item_value_text like 'Low Profile Desktop' or
     item_value_text like 'Lunch Box' or
     item_value_text like 'Main Server chassis' or
     item_value_text like 'Mini Tower' or
     item_value_text like 'Notebook' or
     item_value_text like 'Other' or
     item_value_text like 'Peripherical chassis' or
     item_value_text like 'Pizza box' or
     item_value_text like 'Portable' or
     item_value_text like 'RACK Mount chassis' or
     item_value_text like 'RAID chassis' or
     item_value_text like 'Sealed-case PC' or
     item_value_text like 'Space-saving' or
     item_value_text like 'Sub Notebook' or
     item_value_text like 'Subchassis' or
     item_value_text like 'Tower' or
     item_value_text like 'Unknown')) 'Chassis'

I don't know what I can do, I try

SELECT 
    CASE ISNULL(ítem_value_text, 'NULL')
       WHEN 'NULL' THEN 'No Informado'
       ELSE ítem_value_text
    END

And it doesn't work too, the same thing for inside like this:

WHEN ISNULL(ítem_value_text,'NULL') 
   THEN 'No Informado'

What can I do?

Try this

case isnull(item_value_text,'x')
when 'Low Profile Desktop' then 'Desktop'
when 'Mini Tower' then 'Desktop'
when 'All in One' then 'Desktop'
when 'Portable' then 'Notebook'
when 'LapTop' then 'Notebook'
WHEN 'x' THEN 'No Informado'
when 'Other' then 'Maquina Virtual'
else item_value_text
end as item_value_text

and your where clause should look like below using IN

LIKE from your original code without the wildcard is similar to '=' but since you have so many values to compare then IN is appropriate

where
GIV.object_uuid = DHW.dis_hw_uuid and
 item_name_id=1 AND
 item_root_name_id=1 AND
 item_parent_name_id=2 AND
 item_value_text in ('All in One','....',....)

To simplify:

case  item_value_text   
when 'Low Profile Desktop' then 'Desktop'
when 'Mini Tower' then 'Desktop'
when 'All in One' then 'Desktop'
when 'Portable' then 'Notebook'
when 'LapTop' then 'Notebook'
when 'Other' then 'Maquina Virtual'
else isnull(item_value_text,'No Informado')
end as item_value_text

'NULL' is not the same as NULL . 'NULL' is the string literal consisting of the 4 characters. To test for an unknown value in SQL, you must use IS NULL (or IS NOT NULL ).

item_value_text like 'Tower' or
item_value_text IS NULL

Try Changing you case statement like below

case
        when item_value_text='Low Profile Desktop' then 'Desktop'
        when item_value_text='Mini Tower' then 'Desktop'
        when item_value_text='All in One' then 'Desktop'
        when item_value_text='Portable' then 'Notebook'
        when item_value_text='LapTop' then 'Notebook'
        when item_value_text  is null then 'No Informado'
        when item_value_text='Other' then 'Maquina Virtual'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM