Share via

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 | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Anonymous
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

Was this answer helpful?

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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.