Query the max date?

Vks 40 Reputation points
2023-09-26T08:47:48.3266667+00:00

Hi.

I need to get the max date of the last month in the table. Query can do this?

create table testtbl(EID int,INDATE datetime);

insert into testtbl values(10001,'07/31/2023');
insert into testtbl values(10011,'08/23/2023');
insert into testtbl values(10021,'07/26/2023');
insert into testtbl values(10031,'08/09/2023');
insert into testtbl values(10041,'08/13/2023');
insert into testtbl values(10051,'09/20/2023');
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. PercyTang-MSFT 12,501 Reputation points Microsoft Vendor
    2023-09-26T08:53:14.06+00:00

    Hi @Vks

    If I understand correctly, you need to query ‘08/23/2023'.

    You can try this query.

    select max(INDATE) from testtbl
    where INDATE > eomonth(dateadd(month,-2,getdate()))
      and INDATE <= eomonth(dateadd(month,-1,getdate()));
    

    Best regards,

    Percy Tang

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.