how to find maximum date from sqlserver

Posted by Klbaiju under Sql Server on 11/29/2017 | Points: 10 | Views : 607 | Status : [Member] | Replies : 4
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




Responses

Posted by: Jayakumars on: 11/29/2017 [Member] [MVP] Bronze | Points: 25

Up
0
Down
try this

select top * 1 from tablename order by createdon desc

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anujarishimehta on: 11/30/2017 [Member] Starter | Points: 25

Up
0
Down
Alter the column datatype to datetime from varchar

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kgovindarao523-21772 on: 12/11/2017 [Member] [MVP] Bronze | Points: 25

Up
0
Down
This is your required code:
SELECt MAX(CONVERT(datetime,CreatedOn, 103)) 
FROM YourTable


Thank you,
Govind

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kirthiga on: 5/17/2018 [Member] Starter | Points: 25

Up
0
Down
Create Table C(CreateDate varchar(20))
insert into C values('Jan 24 2017 4:34PM')
insert into C values('Sep 1 2017 2:26PM')
insert into C values('Nov 28 2017 4:37PM')
insert into C values('Jun 29 2017 3:27PM')

select * from C
select max(convert(date,CreateDate,103))CreateDate from C

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response