title: Passing null values while using between in a query
body: <p>I'm writing a stored procedure in T-SQL to select records from the following table:</p>
  
  <pre><code>CREATE TABLE [dbo].[PHM_News_Entry]
  (
      [NewsID] [int] IDENTITY(1,1) NOT NULL,
      [NewsTitle] [varchar](200) NULL,
      [NewsDate] [datetime] NULL,
      [NewsDescription] [nvarchar](max) NULL,
      [NewsDateEntry] [datetime] NULL,
      [NewAuthor] [varchar](200) NULL,
      [NewModificationDate] [datetime] NULL,
      [NewModifiedBy] [varchar](100) NULL,
      [Status] [varchar](50) NULL,
      [NewsAttatchmentPath] [nvarchar](max) NULL,
      [NewsImagePath] [nvarchar](max) NULL,
  )
  </code></pre>
  
  <p>The stored procedure is as follows:</p>
  
  <pre><code>CREATE PROCEDURE Select_News 
      @NewsTitle VARCHAR(200), 
      @FromDate DATE,
      @ToDate DATE
  AS
  BEGIN
      SET NOCOUNT ON;
  
      SELECT * 
      FROM PHM_News_Entry 
      WHERE (NewsTitle LIKE '%'+@NewsTitle+'%' OR @NewsTitle IS NULL) 
        AND NewsDate BETWEEN @FromDate AND @ToDate OR NewsDate IS NULL
  END
  </code></pre>
  
  <p>The parameters viz., @NewsTitle, @FromDate and @ToDate can either have values or be NULL. If all 3 parameters or NULL, there should be no record selected but if one of them has non-null value, it should select one or more matching records.</p>
  
  <p>If I pass NULL to all 3 parameters, it works as expected i.e., doesn't select any records but it's not working the other way i.e., if one of them one or two of them have non-null values.</p>
  
  <p>Please help me write the correct query for this case.</p>
  
  <p>Thanks and regards,</p>
  
  <p>Shyam Singh</p>