anilrajanala
Programmer
How to find just for last month data from a table.
i.e current months is dec,I need rows for Nov.
Thanks in advance
i.e current months is dec,I need rows for Nov.
Thanks in advance
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
-----
DECLARE @dtStart datetime, @dtEnd datetime, @vcMonth varchar(2), @vcYear varchar(4)
---- Get the start date
SET @vcMonth = Month(getdate()) - 1
SET @vcYear = Year(getdate())
SET @dtStart = DATEADD(Month, -1, CAST(@vcMonth + '-1-' + @vcYear as datetime))
---- Get the end date
SET @vcMonth = month(getdate())
SET @dtEnd = DATEADD(Day, -1, CAST(@vcMonth + '-1-' + @vcYear as datetime))
---- Select your records
SELECT col1, col2, col3
FROM table1
WHERE datefield between @dtStart and @dtEnd
-----
DECLARE @dtStart datetime, @dtEnd datetime, @vcMonth varchar(2), @vcYear varchar(4)
SET @vcMonth = Month(getdate())
SET @vcYear = Year(getdate())
---- Get the start date
SET @dtStart = DATEADD(Month, -1, CAST(@vcMonth + '-1-' + @vcYear as datetime))
---- Get the end date
SET @dtEnd = DATEADD(Day, -1, CAST(@vcMonth + '-1-' + @vcYear as datetime))
---- Select your records
SELECT col1, col2, col3
FROM table1
WHERE datefield between @dtStart and @dtEnd