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 7709035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:45:47+00:00 2026-06-01T00:45:47+00:00

I have a line in my SQL Stored Procedure that looks like this (works

  • 0

I have a line in my SQL Stored Procedure that looks like this (works as intended):

HAVING  oh.startdate BETWEEN @startDate AND @endDate

However, further down I have the line:

AND (oh.user IN (@userIDs))

Where @userIDs is a comma delimited string of ID’s and oh.user is an INTEGER, so I must actually put the entire SQL query into a dynamic string (@sql) with all the parameters concatenated into it and then use

sp_executesql @sql 

Everything is working fine except the BETWEEN dates, I’ve tried a few ways and keep getting errors or no results returned when there should be:

    HAVING  oh.startdate BETWEEN CONVERT(DATETIME, '+LEFT(CONVERT(VARCHAR, @startDate, 120), 10)+', 120) AND CONVERT(DATETIME, '+LEFT(CONVERT(VARCHAR, @endDate, 120), 10)+', 120)

returns nothing.

    HAVING  oh.startdate '+LEFT(CONVERT(VARCHAR, @startDate, 120), 10)+' AND '+LEFT(CONVERT(VARCHAR, @endDate, 120), 10)+'

also returns nothing.

    HAVING  oh.startdate BETWEEN ' + @startDate +' AND '+ @endDate +'

returns error converting DATETIME to string.

Any help is appreciated.

Thanks,
Thomas

  • 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-06-01T00:45:49+00:00Added an answer on June 1, 2026 at 12:45 am

    You can use sp_executesql and pass the parameters you want:

    SET @sql = 'SELECT blabla FROM Table WHERE Something HAVING oh.startdate BETWEEN @startDate AND @endDate'
    EXEC sp_executesql @sql,N'@startDate DATE, @EndDate DATE',@startDate, @EndDate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a line of C# in my ASP.NET code behind that looks like
All, I have a stored procedure on SQL Server 2005 that accepts an XML
I have a stored procedure in a SQL Server database that returns a list
The problem is I have a stored procedure that runs consistently in Sql Server
This is stored procedure that worked on earlier version of mysql. Now, I have
We have an application that installs SQL Server Express from the command line and
I have this line in a useful Bash script that I haven't managed to
So I have a for loop that does an iteration of a SQL stored
Does SQL Server 2008 have a built in debugger? I've got a stored procedure
Be gentle, I'm a SQL newbie. I have a table named autonumber_settings like this:

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.