13

Hello I have a SQL statement

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!

6
  • 4
    What does it show if you remove the first line (the INSERT INTO...) ? Jul 18, 2011 at 22:29
  • How do you know the rows with 0 aren't already in the table? I notice you don't really have any WHERE clauses other than the JOIN criteria (this is why it is often better to use an explicit INNER JOIN instead of old-fashioned table,table JOINs). I suggest identifying those rows with a SELECT and leave the destination table out of it for now. Jul 18, 2011 at 22:30
  • I agree with @ypercube, what if you remove the INSERT INTO and replace rewrite the SELECT with SELECT SystemTax.SystemTaxID, EmpowerCalendarCode.CalendarCodeID, EmpowerCalendarCode.CalendarName. Your CASE statement looks ok, so there must be something in your WHERE that is throwing things off.
    – rsbarro
    Jul 18, 2011 at 22:31
  • As per @ypercube, but also select EmpowerCalendarCode.CalendarName just to see what you're getting (ie maybe the join is no good and it's all nulls etc)
    – Bohemian
    Jul 18, 2011 at 22:31
  • Try returning the EmpowerCalendar.CalendarCode column value as well as the case statement, and run the query only (without the insert). This should make sure that the CalendarCode value is what you think it is. Jul 18, 2011 at 22:32

3 Answers 3

12

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.

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?

3
  • 1
    @bluefeet: It's the end of the day and my eyes are tired. I understand the syntax change you made, but I cannot understand how it is functionally different. What am I not seeing? Jul 18, 2011 at 22:56
  • I'm not convinced a functional change "fixed" the problem. I'm guessing the op made other changes to the code (or cleaned up bad data, or both) in addition to the syntax change. Jul 18, 2011 at 22:59
  • @Aaron Bertrand agreed, i don't think it 'fixed' the problem either. I was just offering a better way to write the query without using a WHERE clause
    – Taryn
    Jul 18, 2011 at 23:10
1

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  +  ' %'
0
  1. Try ruling-out any null issues with ISNULL():
  2. Try ruling-out any case-sensitivity issues with UPPER():

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.