SQL query for today's date minus two months - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: Database (https://zeroday.vip/Forum-Database) +---- Forum: Microsoft SQL Server (https://zeroday.vip/Forum-Microsoft-SQL-Server) +---- Thread: SQL query for today's date minus two months (/Thread-SQL-query-for-today-39-s-date-minus-two-months) |
SQL query for today's date minus two months - vieraepdjgqbrv - 07-31-2023 I want to select all the records in a table where their date of entry is older then 2 months. Any idea how I can do that? I haven't tried anything yet but I am on this point: SELECT COUNT(1) FROM FB WHERE Dte > GETDATE() RE: SQL query for today's date minus two months - electees470127 - 07-31-2023 If you are using SQL Server try this: SELECT * FROM MyTable WHERE MyDate < DATEADD(month, -2, GETDATE()) Based on your update it would be: SELECT * FROM FB WHERE Dte < DATEADD(month, -2, GETDATE()) RE: SQL query for today's date minus two months - helaine378 - 07-31-2023 SELECT COUNT(1) FROM FB WHERE Dte > DATE_SUB(now(), INTERVAL 2 MONTH) RE: SQL query for today's date minus two months - svelte204 - 07-31-2023 Would something like this work for you? SELECT * FROM FB WHERE Dte >= DATE(NOW() - INTERVAL 2 MONTH); RE: SQL query for today's date minus two months - neighboresswl - 07-31-2023 SELECT COUNT(1) FROM FB WHERE Dte BETWEEN CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(DATEADD(month, -1, GETDATE())) AS VARCHAR(2)) + '-20 00:00:00' AND CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '-20 00:00:00' RE: SQL query for today's date minus two months - aleece842 - 07-31-2023 TSQL, Alternative using variable declaration. (it might improve Query's readability) DECLARE @gapPeriod DATETIME = DATEADD(MONTH,-2,GETDATE()); --Period:Last 2 months. SELECT * FROM FB as A WHERE A.Dte <= @gapPeriod; --only older records. RE: SQL query for today's date minus two months - regrowing418291 - 07-31-2023 I use this on SQL Server: SELECT DATEADD(MONTH,-2,GETDATE()) '- 2 months' FROM MyTable |