SQL Server - sql query - Asked By Souniel Gaikwad on 10-Jan-11 03:42 AM

sql query to find last date of the column
Nowshad M replied to Souniel Gaikwad on 10-Jan-11 03:54 AM
Hi,.

Try like the below query


Select * from table1 order by dateColumn desc

Get Last date - Lalitha Kumaran replied to Souniel Gaikwad on 10-Jan-11 04:55 AM

I Hope this query would help u ..

idEnv  Name   dtCreatedOn

1   PRD_SIT 2010-03-28 09:18:29.377
2   PRD_UAT 2010-03-28 09:18:29.407
3 TEST2        2010-03-29 09:18:29.407
4 LCGIT      2010-03-30 09:18:29.417
5       PRD              2010-04-06 10:10:26.710

select max(dtcreatedon) from luEnvironment
2010-04-06 10:10:26.710

Anoop S replied to Souniel Gaikwad on 10-Jan-11 04:55 AM
Use TOP and ORDER BY like this way

SELECT TOP 1(datecolum) from tablename ORDER BY ord_date DESC
Sundarasegaram Veerakuddy replied to Souniel Gaikwad on 10-Jan-11 05:14 AM

Hi,

If you look for the last entry of a given column, this may not be the latest date in case of a date column.

If you have any auto-incremented (Identity) column in your table then you can go like,

  SELECT TOP 1 * FROM <table> ORDER BY <auto-incremented column> DESC

Or, if you have any auto-generated system date column (date & time of a row generated), then,

  SELECT TOP 1 * FROM <table> ORDER BY <auto-generated System Date column> DESC

If these kind of supporting columns do not exist in your table, you still have a way of doing it as follows:
(You need to go for a temporary table)

  CREATE TABLE <TmpTable> (IdentityCol Int Identity(1,1), YourCol1, YourCol2...)

then,

  INSERT INTO <TmpTable> (YourCol1, YourCol2...) SELECT YourCol1, YourCol2... FROM <YourTable>

and then,

  SELECT TOP 1 * FROM <TmpTable> ORDER BY IdentityCol DESC

Hope this assists.

Daivagna Nanavati replied to Souniel Gaikwad on 11-Jan-11 12:54 PM
Hi Souniel

If you want only one record then you can do like following

SELECT top 1 CreatedOn FROM tblEmp ORDER BY CreateOn DESC

you can relate it to your table name and field

let me know

Thanks