SQL Server SQL query for getting data for last 3 months

xwmevbvl  于 2023-02-28  发布在  其他
关注(0)|答案(5)|浏览(149)

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 month as 01/mm/yyyy .

dba5bblo

dba5bblo1#

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())
nwwlzxa7

nwwlzxa72#

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)
iezvtpos

iezvtpos3#

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

SELECT *
FROM   mytable
WHERE  DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
wqsoz72f

wqsoz72f4#

Last 3 months

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

Today

SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
p5fdfcr1

p5fdfcr15#

Last 3 months record

SELECT *, DATEDIFF(NOW(),`created_at`)as Datediff from `transactions` as t having Datediff <= 90

相关问题