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

The Archive Base Latest Questions

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

Hi all wonder if someone could advise a more efficient way to select rows

  • 0

Hi all wonder if someone could advise a more efficient way to select rows from a table that has roughly 60 millions records in it. Each row has a date stored as a nvarchar, for example ‘20110527030000.106’. I want to select all rows that are 3 months or older based on this date field, so for example i’m only interested in the first part of the date field; ‘20110527’. I have the following code to do that, however its a bit slow and wondering if there was a better way?

DECLARE @tempDate varchar(12)
SET @tempDate = convert(varchar(12),DATEADD(m,-3,GETDATE()),112)

SELECT *
  FROM [TABLE A]
  WHERE SUBSTRING([DATE_FIELD],0,8) < @tempDate
  • 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-25T09:38:45+00:00Added an answer on May 25, 2026 at 9:38 am

    Your query not only it can’t use any index on [DATE_FIELD] and does a full scan but it also applies the SUBSTRING() function to all values of the (date_field column of the) table.

    Don’t apply any function on the column so the index of [DATE_FIELD] can be used and the function is only applied once, at the calculation of @tempDate :

    SELECT *
      FROM [TABLE A]
      WHERE [DATE_FIELD] < @tempDate
    

    The < comparison works for varchar values. The following will evaluate to True:

    '20110526030000.106' < '20110527'
    

    Is there any reason that the datetime is not stored as datetime type?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder whether someone could help me please, From a tutorial I found online
Hi all i'm struggling with this a bit and wonder if someone could lend
I wonder, if I can inject a list of (stateless) beans, that all implementing
This is a total newbie question, but I wonder if someone could assist with
I wonder if someone could offer insight here: I work for an independent multimedia
I wonder if someone could explain why a while loop treats multipe test conditions
I've been battling with this issue all day. I am hoping someone has an
I've found a useful function on another answer and I wonder if someone could
I wonder if someone could tell me how to make it possible to move
I wonder if someone knows where can I find a list of all the

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.