SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Query to get records belongs to the previous month

    Hai folks,

    i have records of transactions in a table.
    the date of transactions have the below format

    field name : dt
    data type : varchar 255
    ex: 2012-11-13

    now i need to get all the transactions belongs to the previous months :

    i had the following and was working only until December 31st

    Code:
    $m=date('m');
    $query="SELECT COUNT(tid) as tot
            FROM transactions
    		WHERE MONTH(dt) < $m";
    pls help me . urgent!

  2. #2
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you folks ,
    just found the answer

    Code:
    $query="SELECT COUNT(tid) as tot
            FROM transactions
    		WHERE DATE_FORMAT(dt,'%Y-%m-%d') > DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01')";
    Edited : Oops, answer seems wrong.

    Code:
    SELECT DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01')
    Returns 2012/12/01, but expected 2012/12/31

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by afridy View Post
    Code:
    SELECT DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01')
    Returns 2012/12/01, but expected 2012/12/31
    why would you expect 2012/12/31 when you explicitly return 01 as the day?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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