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

  • SEARCH
  • Home
  • 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 3357504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:38:27+00:00 2026-05-18T02:38:27+00:00

I have UI where a user can search on optional parameters.I am writing a

  • 0

I have UI where a user can search on optional parameters.I am writing a SP and as a testing database I am using AdventureWorks.

For some reasons I dont seem to find a way how to include

Where ModifiedDate between ”’ + @Fromdate + ”’ And ”’+ @ToDate + ””)

in my existing SP.Can you help?

My sp looks like this

 ALTER PROCEDURE SearchPerson_Dynamic
(
@FirstName NVARCHAR(50) = NULL, 
@LastName NVARCHAR(50) = NULL, 
@EmailAddress NVARCHAR(50) = NULL, 
@Phone NVARCHAR(25) = NULL,
@FromDate DATETIME =NULL,
@ToDate DATETIME =NULL

AS
DECLARE @sSQL NVARCHAR(MAX), @Where NVARCHAR(1000) = ''
SET @sSQL = 'SELECT * FROM [Person].[Contact]'

IF @FirstName is not null
SET @Where = @Where + 'AND FirstName = @_FirstName '
IF @LastName is not null
SET @Where = @Where + 'AND LastName = @_LastName '
IF @EmailAddress IS NOT NULL
SET @Where = @Where + 'AND EmailAddress = @_EmailAddress '
IF @Phone IS NOT NULL
SET @Where = @Where + 'AND Phone = @_Phone '

-- NEED TO INTEGRATE A SEARCH BETWEEN @FromDate and @ToDate 

IF LEN(@Where) > 0
SET @sSQL = @sSQL + 'WHERE ' + RIGHT(@Where, LEN(@Where)-3)

 EXEC sp_executesql @sSQL,
    N'@_FirstName VARCHAR(50), 
            @_LastName VARCHAR(50), 
            @_EmailAddress VARCHAR(50), 
            @_Phone NVARCHAR(25)',  
    @_FirstName = @FirstName, 
            @_LastName = @LastName, 
            @_EmailAddress = EmailAddress,
            @_Phone = @Phone
            MISSING @FROMDate and @ToDate  --HOW DO i DO IT HERE

One thing I have not done is that some fields EG Surname the user can select “ALL” in the UI from the drop down. How would I implement a search where I dont have an exact parameter?

Thanks again for your help

  • 1 1 Answer
  • 1 View
  • 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-18T02:38:28+00:00Added an answer on May 18, 2026 at 2:38 am

    If I was to rewrite your stored proc, I would do the following

    ALTER PROCEDURE SearchPerson_Dynamic
    (
    @FirstName NVARCHAR(50) = NULL, 
    @LastName NVARCHAR(50) = NULL, 
    @EmailAddress NVARCHAR(50) = NULL, 
    @Phone NVARCHAR(25) = NULL,
    @FromDate DATETIME =NULL,
    @ToDate DATETIME =NULL
    )
    as
    begin
    
        SELECT * FROM [Person].[Contact]
        where
            FirstName = coalesce(@FirstName, FirstName) and
            LastName = coalesce(@LastName, LastName) and
            EmailAddress = coalesce(@EmailAddress, EmailAddress) and
            Phone = coalesce(@Phone, Phone) and
            ModifiedDate >= coalesce(@FromDate, FromDate) and
            ModifiedDate <= coalesce(@ToDate, ToDate)
    end
    

    This is better as its not executing text for the stored procedure and could be optimized.

    If you want to use the dynamic sql, you could cast the date to a normal string, ie

    IF @FromDate IS NOT NULL AND @ToDate IS NOT NULL
        SET @Where = @Where + 'AND ModifiedDate between ''' + cast( @FromDate as varchar(50) ) +  ''' and  ''' +  cast( @ToDate as varchar(50) ) +  ''' '
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my homepage I have a simple search form. The user can enter some
I have created a form where the user can search the database, and the
I'm creating a dictionary database using php and MySQL. The user can search for
i have the following problem : I'm using Jquery mobile , user can search
I have a search form where user can search for products within their specified
i have a search page in my site, where user can search all the
I have a problem with my search query, $s_query. The user can search a
Okay, so I have a website where the user can put a search query
I have the following problem: I have a search box in which user can
i have a single textbox named Keywords. User can enter multiple strings for search.

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.