February 21, 2019 at 1:32 pm
There is a column [OrderDate] store data as varchar format as 'MM/dd/yyyy'.
For example,
04/02/2018
02/15/2019
12/31/2017
How to code to select max date which is '02/15/2019' ?
February 21, 2019 at 1:35 pm
convert to date
select max converted date
and fix your tables - dates should never be stored as anything other than dates .
February 21, 2019 at 3:38 pm
frederico is right, you should change the table definition to store date type values. But you can order that string in date orderSELECT TOP(1) *
FROM dbo.myTable
ORDER BY RIGHT(OrderDate, 4) + SUBSTRING(OrderDate, 4, 2) + LEFT(OrderDate, 2) DESC
orSELECT TOP(1) *
FROM dbo.myTable
ORDER BY convert(date,OrderDate,101) DESC
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply