Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:38:12+00:00 2026-05-15T09:38:12+00:00

I need help in resolving a complex SQL query. I am trying to build

  • 0

I need help in resolving a complex SQL query. I am trying to build up the query one vlock at a time.
One issue is: If a parameter for @PubNum is NULL, the query shows “….. where PubNum = ” which is an issue. What I need is if the parameter is NULL, then PubNum should not be in the where clause.

A second issue is:

  • If @StartDate IS NOT NULL and @EndDate IS NOT NULL THEN RecAddDate BETWEEN @StartDate AND @EndDate
  • If @StartDate IS NOT NULL and @EndDate IS NULL THEN RecAddDate BETWEEN @StartDate AND Today
  • If @StartDate IS NULL and @EndDate IS NOT NULL THEN RecAddDate BETWEEN ’01/01/2000′ AND @EndDate
  • If @StartDate IS NULL and @EndDate IS NULL THEN RecAddDate BETWEEN ’01/01/2000′ AND Today

Any ideas?

The complete query is:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[usp_BookItemSearch]
    @BookSKU varchar(30) = NULL
    ,@SearchType int = NULL
    ,@PubNum varchar(10) = NULL
    ,@UserID int = NULL
    ,@StartDate smalldatetime = NULL
    ,@EndDate smalldatetime = NULL
AS
DECLARE @SQL as varchar(4000)

SET @SQL = 'SELECT RecID, PubNum, VendorName, InvoiceNum, BookSKU, RecAddDate FROM tb_BookInventoryLog]'

IF @BookSKU IS NOT NULL
    BEGIN
        IF  @SearchType = 2
            BEGIN
                SET @SQLClause = ' WHERE BookSKU LIKE ''%' + @BookSKU + ''''    --Ends with
            END
        IF  @SearchType = 1
            BEGIN
                SET @SQLClause = ' WHERE BookSKU LIKE ''%' + @BookSKU + '%'''   --Contains
            END
        IF  @SearchType = 0
            BEGIN
                SET @SQLClause = ' WHERE BookSKU LIKE ''' + @BookSKU + '%'''    --Starts with
            END
    END

IF @PubNum IS NOT NULL
    BEGIN
        IF @SQLClause IS NOT NULL
            BEGIN
                SET @SQLClause = @SQLClause + ' AND PubNum = ''' + @PubNum + ''''
            END
        ELSE
            BEGIN
                SET @SQLClause = @SQLClause + ' WHERE PubNum = ''' + @PubNum + ''''
            END
    END

IF @UserID IS NOT NULL 
    BEGIN
        IF @SQLClause IS NOT NULL
            BEGIN
                SET @SQLClause = @SQLClause + ' AND (UserID = ' + CAST(@UserID AS VarChar) + ')'
            END
        ELSE
            BEGIN
                SET @SQLClause = @SQLClause + ' WHERE (UserID = ' + CAST(@UserID AS VarChar) + ')'
            END
    END

If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
    BEGIN
            Set @SQLClause = @SQLClause + ' And (JoiningDate BETWEEN @StartDate AND @EndDate)'
    END

IF (@EndDate IS NOT NULL)
    BEGIN
        IF (@StartDate IS NOT NULL) 
            BEGIN
                SET @SQL = @SQL +  ' WHERE RecAddDate between' + CAST(@StartDate As smalldatetime) + ' AND ' + CAST(@EndDate as smalldatetime) + ''
            END
        ELSE
            BEGIN
                SET @SQL = @SQL +  ' RecAddDate BETWEEN 01/01/2000 AND @EndDate + '
            END
    END

SET @SQL = @SQL + @SQLClause + ' ORDER BY BookSKU, PubNum'
PRINT @SQL
--EXECUTE (@SQL)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:38:13+00:00Added an answer on May 15, 2026 at 9:38 am

    Instead of doing dynamic SQL (which introduces a whole number of problems, and often isn’t actually necessary), you could just use the parameters and some NULL checks as part of your WHERE clause using a couple of different techniques, depending on what you’re trying to do. I tested this using SQL 2000/2005, and it works correctly (I’d also assume it’s fine in 2008/R2).

    ALTER PROCEDURE [dbo].[usp_BookItemSearch]
        @BookSKU varchar(30) = NULL
        ,@SearchType int = NULL
        ,@PubNum varchar(10) = NULL
        ,@UserID int = NULL
        ,@StartDate smalldatetime = NULL
        ,@EndDate smalldatetime = NULL
    AS
    
    
      SELECT RecID, PubNum, VendorName, InvoiceNum, BookSKU, RecAddDate
        FROM [tb_BookInventoryLog]
       WHERE (
              (@BookSKU IS NULL) OR 
              (BookSKU LIKE CASE @SearchType
                              WHEN 0 THEN       @BookSKU + '%'
                              WHEN 1 THEN '%' + @BookSKU + '%'
                              WHEN 2 THEN '%' + @BookSKU
                            END
              )
             )
         AND ISNULL(@PubNum, PubNum) = PubNum
         AND ISNULL(CAST(@UserID AS VARCHAR), UserID) = UserID
         AND (
              (@StartDate IS NULL OR @EndDate IS NULL) OR
              (JoiningDate BETWEEN @StartDate AND @EndDate)
             )
         AND RecAddDate BETWEEN CASE
                                  WHEN @EndDate IS NULL THEN RecAddDate
                                  ELSE ISNULL(@StartDate, '01/01/2000')
                                END
                            AND ISNULL(@EndDate, GETDATE())
    ORDER BY BookSKU, PubNum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An instance of a Doctrine model class corresponds to one… May 15, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer Probably due to this option (register_argc_argv). May 15, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer Not sure if this is possible with WebChromeClient, but I… May 15, 2026 at 3:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.