SQL query for getting data for last 3 months

前端 未结 4 1432
一个人的身影
一个人的身影 2020-12-03 07:08

How can you get today\'s date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery

相关标签:
4条回答
  • 2020-12-03 07:19
    SELECT * 
    FROM TABLE_NAME
    WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE()) 
    

    Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.

    or you can check against last 90 days.

    SELECT * 
    FROM TABLE_NAME
    WHERE Date_Column >= DATEADD(DAY, -90, GETDATE()) 
    
    0 讨论(0)
  • 2020-12-03 07:27

    Latest Versions of mysql don't support DATEADD instead use the syntax

    DATE_ADD(date,INTERVAL expr type)
    

    To get the last 3 months data use,

    DATE_ADD(NOW(),INTERVAL -90 DAY) 
    DATE_ADD(NOW(), INTERVAL -3 MONTH)
    
    0 讨论(0)
  • 2020-12-03 07:38

    I'd use datediff, and not care about format conversions:

    SELECT *
    FROM   mytable
    WHERE  DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
    
    0 讨论(0)
  • 2020-12-03 07:39

    Last 3 months

    SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
    

    Today

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
    
    0 讨论(0)
提交回复
热议问题