简体   繁体   English

SQL 中的案例语句使用 Like

[英]Case Statement in SQL using Like

Hello I have a SQL statement你好我有一个 SQL 声明

INSERT INTO Foundation.TaxLiability.EmpowerSystemCalendarCode

SELECT SystemTax.SystemTaxID,
       EmpowerCalendarCode.CalendarCodeID
      ,CASE WHEN  EmpowerCalendarCode.CalendarName LIKE '%Monthly%' THEN 3
            WHEN  EmpowerCalendarCode.CalendarName LIKE '%Annual%' THEN 2
            WHEN  EmpowerCalendarCode.CalendarName LIKE '%Quarterly%' THEN 4
       ELSE 0
       END
       FROM Foundation.Common.SystemTax SystemTax, Foundation.TaxLiability.EmpowerCalendarCode EmpowerCalendarCode
WHERE SystemTax.EmpowerTaxCode = EmpowerCalendarCode.LongAgencyCode and SystemTax.EmpowerTaxType = EmpowerCalendarCode.EmpowerTaxType

Even though CalendarName has values like Quarterly (EOM) I still end up getting 0. Any ideas and suggestions!即使 CalendarName 具有 Quarterly (EOM) 之类的值,我仍然最终得到 0。任何想法和建议!

For one, I would update your SQL to this so you are using a JOIN on your SELECT statement instead of placing this in a WHERE clause.一方面,我会将您的 SQL 更新为此,因此您在SELECT语句上使用JOIN而不是将其放在WHERE子句中。

INSERT INTO Foundation.TaxLiability.EmpowerSystemCalendarCode

SELECT SystemTax.SystemTaxID,
       EmpowerCalendarCode.CalendarCodeID
      ,CASE WHEN  EmpowerCalendarCode.CalendarName LIKE '%Monthly%' THEN 3
            WHEN  EmpowerCalendarCode.CalendarName LIKE '%Annual%' THEN 2
            WHEN  EmpowerCalendarCode.CalendarName LIKE '%Quarterly%' THEN 4
       ELSE 0
       END
FROM Foundation.Common.SystemTax SystemTax
INNER JOIN Foundation.TaxLiability.EmpowerCalendarCode EmpowerCalendarCode
    ON SystemTax.EmpowerTaxCode = EmpowerCalendarCode.LongAgencyCode 
    AND SystemTax.EmpowerTaxType = EmpowerCalendarCode.EmpowerTaxType

two, what happens if you remove the INSERT INTO ?二,如果你删除INSERT INTO会发生什么?

You can also do like this.你也可以这样做。 I think this will work.我认为这会奏效。

select *
from table
where columnName like '%' + case when @varColumn is null then '' else @varColumn end  +  ' %'
  1. Try ruling-out any null issues with ISNULL():尝试用 ISNULL() 排除任何 null 问题:
  2. Try ruling-out any case-sensitivity issues with UPPER():尝试用 UPPER() 排除任何区分大小写的问题:

    CASE WHEN upper(isnull(EmpowerCalendarCode.CalendarName, 'none')) LIKE '%MONTHLY%' THEN 3... CASE WHEN upper(isnull(EmpowerCalendarCode.CalendarName, 'none')) LIKE '%MONTHLY%' THEN 3...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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