WebApr 1, 2024 · 1 Answer Sorted by: 2 The clearest and least error prone way to do this is as follows: Use greater then equals >= for the lower limit Use less than < for the upper limit, and make that limit the first of the next month WHERE ap_CreatedDate >= DATEADD (DAY, 1, EOMONTH (GETDATE (), -2)) AND ap_CreatedDate < DATEADD (DAY, 1, … WebMar 14, 2024 · If it's the last day of the quarter before last quarter you can use the EOMONTH function to turn this date into the last day of the month rather than the first You could also consider taking your current date, adding ( ( (MONTH (date) - 1) % 3) - 6) months to it then EOMONTHing it.
sql - tsql: How to retrieve the last date of each month between given ...
WebIf you want to get data for the past X number of months then just use DATEADD it's a very useful function. All you need to do then is select YourColumns FROM YourTable WHERE YourDate >= DATEADD (MONTH, -13, CAST (GETDATE () AS DATE)) and bam there you go. Share Improve this answer Follow answered Apr 18, 2014 at 14:29 Zane 4,129 1 20 35 WebApr 29, 2024 · To get the last day of the month of a given date, a query is as follow: Example - To get the last day of the month of a given date using EOMONTH DECLARE @Date1 datetime; SET @Date1 = '04/27/2024'; SELECT EOMONTH (@Date1) as 'Last Day of Month'; Output Last Day of Month ----------------------- 2024-04-30 chill clothing whitefish
SQL Query to Get Previous Months Records in SQL Server
WebNov 16, 2024 · SELECT * FROM YourTable WHERE [A header] >= YEAR (DATEADD (month, -12, GETDATE ())) * 100 + MONTH (DATEADD (month, -12, GETDATE ())) db<>fiddle This is made worse by the fact you are storing dates as integers. Instead you should store the last day of the month as a date type, then you can do WHERE [A … WebHere's the last day of month code i'm currently using: declare @date datetime set @date='1/4/13' select DATEADD (d, -1, DATEADD (m, DATEDIFF (m, 0, @date) + 1, 0)) sql sql-server-2008 Share Follow asked Jan 10, 2014 at 21:21 FistOfFury 6,587 7 49 57 Add a comment 7 Answers Sorted by: 5 Quite a neat solution: WebTo get the previous month in SQL Server, subtract one month from today's date and then extract the month from the date. First, use CURRENT_TIMESTAMP to get today's date. Then, subtract 1 month from the current date using the DATEADD function: use MONTH as the date part with -1 as the parameter. grace collins facebook