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
  • 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-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

I have a sql database that stores some documents. A user can sign into
We have a page of search results which the user can hit in several
On a website if I have a form where the user can input some
Well i need to make it so any user can have a foo.com/username in
I have a web application with users and their documents. Each user can have
I have a form a user can enter their name, then it will add
I have a page upon which a user can choose up to many different
I have a form in which the user can choose a component type from
I want to have a text box that the user can type in that
I have a Timestamp value that comes from my application. The user can be

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.