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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:05:35+00:00 2026-05-19T01:05:35+00:00

I am querying tables from Microsoft SQL 2008 which have date split across 3

  • 0

I am querying tables from Microsoft SQL 2008 which have date split across 3 columns: day, month and year. Unfortunately, I do not have control over this because data is coming in to the database daily from a 3rd party source in that format.

I need to add between to a where clause so user can pull records within a range. Would be easy enough if date was in a single column but finding it nearly impossible when its split across three columns.

To display the date, I am doing a

CAST(
    CAST(year as varchar(4)) + '-' + 
    CAST(month as varchar(2)) + '-' + 
    CAST(day as varchar(2))
as date) AS "date"`  

in a select. I tried to put it as a parameter for datediff function or just the regular between but get no results.

  • 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-19T01:05:36+00:00Added an answer on May 19, 2026 at 1:05 am

    Without access to change the table, you can use a subquery to construct the date before the filtration is applied:

    SELECT x.*
      FROM (SELECT CAST(CAST(year as varchar(4)) + '-' + 
                        CAST(month as varchar(2)) + '-' + 
                        CAST(day as varchar(2)) AS date) AS [date]
                        ...
              FROM ...) x
     WHERE x.[date] BETWEEN ? AND ?
    

    If using SQL Server 2005+, you can use a Common Table Expression (CTE) instead — but there’s no performance difference between using a CTE vs the subquery approach:

    WITH example AS (
       SELECT CAST(CAST(year as varchar(4)) + '-' + 
                   CAST(month as varchar(2)) + '-' + 
                   CAST(day as varchar(2)) AS date) AS [date]
                   ...
         FROM ...) 
    SELECT x.*
      FROM example x
     WHERE x.[date] BETWEEN ? AND ?
    

    Caveats:

    Because the dates are being constructed on the fly, this will never perform as well as if the values were stored as DATE or DATETIME — an index on the year, month or day columns can not be utilized. Options to consider would be an indexed view (AKA materialized view), or computed column.

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

Sidebar

Related Questions

Is there some means of querying the system tables to establish which tables are
I am querying information from Active Directory . I have code that works, but
I have a simple problem when querying the SQL Server 2005 database. I have
I'm trying to do quite a lot of querying against a Microsoft SQL Server
I'm querying a bunch of information from cisco switches using SNMP. For instance, I'm
The db I am querying from is returning some null values. How do I
I am querying data from views that are subject to change. I need to
Is there any framework for querying XML SQL Syntax, I seriously tire of iterating
I have recently written a dynamic querying tool using expression trees and as I
I have designed a database for an application which contains user settings. This database

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.