Results 1 to 4 of 4

Thread: [RESOLVED] Select between 2 "dates"

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Resolved [RESOLVED] Select between 2 "dates"

    Hello guys,

    I have a DB table where the date is represented in 6 separate integer fields/columns (year, month, day, hour, minute, second). Don't ask me why and please do not tell me to change the column types... It's something I have no control over. Now my question is, how do I create a query to select all records between 2 dates in this table?
    I've tried this but it doesn't return the right records.

    Code:
    "Select * from tablename where (yr >= @year1 and month >= @month1 and day >= @day1) 
                                       and (yr <= @year2 and month <= @month2 and day <= @day2)
    Any help is greatly appreciated.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select between 2 "dates"

    What about converting to string (varchar) and then Convert to DateTime \

    CONVERT(DATETIME(CONVERT(VARCHAR(2),month) + '/' + Convert(VARCHAR(2),day) + '/' + Convert(Varchar(4),Year) )
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Select between 2 "dates"

    sql Code:
    1. SELECT * FROM TableName
    2. WHERE (yr > @year1 OR (yr = @year1 AND (month > @month1 OR (month = @month1 AND day >= @day1)
    3. AND (yr < @year2 OR (yr = @year2 AND (month < @month2 OR (month = @month2 AND day <= @day2)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] Select between 2 "dates"

    @JMC: Thanks, JMC. That did the trick.

    @Gary: your suggestion probably works in most cases but the problem is the DB I'm working with (there are some records that that the "day" field has no data and thus it default to zero - so problems arise when it tries to convert "12/00/2010" to a date Using JMC's query, I was able to pull these records and fix them as needed. Thanks anyway.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width