2014-05-25 3 views
0

아래 문을 쿼리했지만 like 문이 char (4) 열 (코드)에서 작동하지 않습니다. 왜? 어떻게 해결할 수 있습니까? 코드에서 @code가 좋아하는 모든 프로젝트를 검색하고 싶습니다. 코드 예제 프로젝트를위한 'FB01', 'FB02', ...SQL Server like statement with char column

DECLARE @Code char(4) = 'FB' , 

@CustomerID int = null , 

@Name nvarchar (200) = null , 

@CompanyID int = null , 

@Status bit = null , 

@ProjectStatusID int = null, 

@OrderBy nvarchar(50) = null , 

@StartRowIndex int , 

@MaximumRows int; 


       WITH CompanyToProjectResult([ProjectID]) 
       AS 
       (
        SELECT [ProjectID] 
        FROM [CompanyToProject] 
        WHERE 
        ([CompanyToProject].[CompanyID]= @CompanyID OR @CompanyID IS NULL) 

        GROUP BY [ProjectID] 
       ) 

       SELECT 
        [Project].[ID] ID 
        ,[Project].Code Code 
        ,[Project].Name Name 
        ,[Customer].Name Customer 
        ,[Project].Status Status 
        ,[ProjectStatus].Name ProjectStatus 
        ,COUNT([Project].[ID]) OVER() [TotalRows] 
       FROM 
        [Project] 
        LEFT JOIN [Customer] ON [Project].[CustomerID] = [Customer].[ID] 
        LEFT JOIN [CompanyToProjectResult] ON [Project].[ID] = [CompanyToProjectResult].[ProjectID] 
        LEFT JOIN [ProjectStatus] ON [Project].[ProjectStatusID] = [ProjectStatus].[ID] 
       WHERE 
        (([Project].[Code] Like '%' + @Code + '%') OR @Code IS NULL) 
        AND ([CustomerID] = @CustomerID OR @CustomerID IS NULL) 
        AND ([Project].[Name] Like '%' + @Name + '%' OR @Name IS NULL) 
        AND ([CompanyToProjectResult].[ProjectID] IS NOT NULL OR @CompanyID IS NULL) 
        AND ([Status] = @Status OR @Status IS NULL) 
        AND ([ProjectStatusID] = @ProjectStatusID OR @ProjectStatusID IS NULL) 

        ORDER BY COALESCE(@OrderBy, '[Project].[ID] DESC') 
        OFFSET 0 ROWS 
        FETCH NEXT 9 ROWS ONLY 

답변

3

사용 varchar 대신 char. char(4) = 'FB''FB '

DECLARE @Code varchar(4) = 'FB'