简体   繁体   中英

Find Maximum date SQL Server

I am using below query to find max date,getting output as "30-12-2017" , whereas the output should be "15-12-2018" .my sample data is attached, data type is nvarchar and SQL Server 2008.

select MAX(date1) from tblMonth

在此处输入图片说明

Your datatype for Date1 seems to be nvarchar . The MAX() on nvarchar 's is a string compare. The string 30-12-2017 starts with 30 , which has the biggest ASCII code in your example. If you enter 31-11-2017 , this would be the result of MAX(date1) .

Use a date datatype (ie datetime2 or date ) instead and it will work as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM