0

When I run the following code without setting any parameters I get the desired output

    SELECT * FROM (
    SELECT [CaseNumber]
          ,[EventCode]
          ,[EventDate]
          ,[Assigned]
          ,ROW_NUMBER() OVER (
                                    PARTITION BY CaseNumber
                                    ORDER BY CaseNumber,EventDate DESC,EventCode  
                            ) AS [ROW NUMBER] 
      FROM  [databasename].[dbo].[tblCsCaseEvents] 
      ) groups

    WHERE 1=1 
    AND (groups.[ROW NUMBER]=1) AND (Assigned IN ('PARKERE')) 
    AND  EventCode LIKE 'ba%'

However, when I declare and set variables as shown below and run it. The script doesn't seem to work. Why is that the case?

Declare @attorney CHAR (10)
Declare @event CHAR (10)

set @attorney='PARKERE'
set @event='ba%'

SELECT * FROM (
SELECT [CaseNumber]
      ,[EventCode]
      ,[EventDate]
      ,[Assigned]
      ,ROW_NUMBER() OVER (
                                PARTITION BY CaseNumber
                                ORDER BY CaseNumber,EventDate DESC,EventCode  
                        ) AS [ROW NUMBER] 
  FROM  [databasename].[dbo].[tblCsCaseEvents] 
  ) groups

WHERE 1=1 
AND (groups.[ROW NUMBER]=1) AND (Assigned IN (@attorney)) 
AND  EventCode LIKE @event
1
  • 1
    What error are you getting? Commented Aug 22, 2019 at 15:09

1 Answer 1

3

char(<n>) defines a string of exactly a length <n>. If the string is too short, it is padded with spaces.

This affects some comparisons, such as like. Instead, use varchar().

Here is an explanation from Microsoft.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.