Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I am loading data into table...
One of the columns is HireDate which is as string, i.e. 20220530
20220421
20220607
...
Question,
What is the select query to return all rows but for previous month
i.e. where HireDate >= 1st of previous month and HireDate < = last day of previous month
Thank you
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Answer accepted by question author
Check this condition:
select *
from MyTable
where datediff(month, HireDate, getdate()) = 1
SELECT *
FROM test
where HireDate >=DATEADD(mm, DATEDIFF(mm,0,GETDATE())-1,0)
and HireDate <DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0)
DECLARE @StartDate DATE = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', CURRENT_TIMESTAMP)-1, '19000101'); -- beginning of the previous month
DECLARE @EndDate DATE = EOMONTH(@StartDate)
select * from InfoTable where HireDate between @StartDate and @EndDate