Results 1 to 4 of 4

Thread: Extract from table all row between date

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Posts
    18

    Extract from table all row between date

    Hi,

    I need to extract from a table a rows from:

    3 to 6 months
    over 12 months

    I have done the queries but did not work:

    1)SELECT data_pick FROM table WHERE data <=NOW()-INTERVAL 3 month AND 6 month;
    2)SELECT data_pick FROM table WHERE data <=NOW() > 12 month;

    Thanks in advance.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Extract from table all row between date

    Wrong Forum. This belongs at Database

    To your Question: Which DBMS?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    Re: Extract from table all row between date

    Quote Originally Posted by hamon View Post
    I need to extract from a table a rows from:

    3 to 6 months
    over 12 months
    You can almost certainly do this with database functions, but perhaps the quickest way is to have your Application work out the dates involved and run the queries with those dates in them:

    Code:
    ' 3..6 months 
    dtFrom = DateAdd( "m", -6, Today() )
    dtTo = DateAdd( "m", -3, Today() )
    sSQL = "select a, b, c from table1 where value between " _
       & Format$( dtFrom, "'yyyy-mm-dd'" ) _
       & " and " _
       & Format$( dtTo, "'yyyy-mm-dd'" ) _
       & " order by value" 
    
    ' Older than 12 months 
    dtTo = dtTo = DateAdd( "m", -12, Today() )
    sSQL = "select a, b, c from table1 where value < " _
       & Format$( dtFrom, "'yyyy-mm-dd'" ) _
       & " order by value"
    Of course, you should use Parameterised Queries; the above air-code is only an example.

    Regards, Phill W.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Extract from table all row between date

    Quote Originally Posted by hamon View Post
    Hi,

    I need to extract from a table a rows from:

    3 to 6 months
    over 12 months

    I have done the queries but did not work:

    1)SELECT data_pick FROM table WHERE data <=NOW()-INTERVAL 3 month AND 6 month;
    2)SELECT data_pick FROM table WHERE data <=NOW() > 12 month;

    Thanks in advance.
    First study the syntax for queries.
    For your current case you could use the BETWEEN operator:
    https://www.w3schools.com/sql/sql_between.asp

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