You must Sign In to post a response.
  • Category: SQL Server

    Find maximum date using sqlserver

    Hi All,

    I have a table with these datas

    CreatedOn
    Jan 24 2017 4:34PM
    Mar 31 2017 5:34PM
    Feb 27 2017 4:57PM
    May 2 2017 1:14PM
    Jun 29 2017 3:27PM
    Sep 1 2017 2:26PM
    Nov 28 2017 4:37PM
    My requirement is to select the maximum date from this table.

    the datafield type is nvarchar(50).

    when I try to fetch the maximum date using following query

    select MAX(createdon) from tablename,Iam getting following result

    (No column name)
    Sep 1 2017 2:26PM

    why it is displaying september instead of november

    Regards

    Baiju
  • #769440
    Hi

    try this query


    Select top * 1 from tablename order by CreatedOn Desc

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #769442
    Hi Baiju.

    You can't use max aggregate function to get date.
    Try below code which will give you the expected output.

    select top 1 * from table_name order by CAST(created_on as datetime) desc
    or
    select top 1 created_on from table_name order by CAST(created_on as datetime) desc

    out put will be Nov 28 2017 4:37PM

    Sridhar Thota.
    Editor: DNS Forum.


  • Sign In to post your comments