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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:32:34+00:00 2026-05-26T02:32:34+00:00

I tried to figure out a dynamic query to get date col within past

  • 0

I tried to figure out a dynamic query to get date col within past 20 days. The idea is quite simple and, moreover, I know the table does contain dates from getdate() to -20 days but still no result get returned

DECLARE @date_past_period varchar(MAX);
DECLARE @date_past_number varchar(MAX);
SET @date_past_period='day';
SET @date_past_number='20';

DECLARE @aDate datetime;

DECLARE @sql varchar(MAX);
SET @sql='SELECT date FROM table WHERE convert(varchar,date,121) BETWEEN convert(varchar,getdate(),121) AND convert(varchar,dateadd('+@date_past_period+', -'+@date_past_number+', getdate()),121)';


exec(@sql);

Maybe the problem is in dynamic thing but I am not sure.

Any useful comment is appreciated

  • 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-26T02:32:34+00:00Added an answer on May 26, 2026 at 2:32 am

    I am pretty sure this scenario can be covered without using dynamic SQL, however, one obvious problem in your SQL is the between clause – the range is in the wrong order. Try changing your @sql as below:

    SET @sql='SELECT date FROM table WHERE convert(varchar,date,121) BETWEEN convert(varchar,dateadd('+@date_past_period+', -'+@date_past_number+', getdate()),121) AND convert(varchar,getdate(),121)';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I tried to figure out is this possible in any way. Here is
I have tried to figure out how to disable the header for a jqGrid,
I recently tried to figure out how to use freeglut with NetBeans 7. I
I have tried to figure this out and googled through a fair few stack
I've tried my best and cannot figure out what happened here. It worked fine
I cannot figure out why appendChild is returning undefined. I also tried t.previousSibling.appendChild(document.createElement('span')); which
After trying to figure out why a Capistrano task (which tried to start a
I can't figure out why this won't work, I've tried it 100 ways. The
I am trying to figure out the issue, and tried different styles that I
I have tried for a very long time now to figure out, why 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.