Avatar of Ernesto
ErnestoFlag for Mexico asked on

Get max date from a varchar in SQL query!

I need to get the max date from  a varchar of a table in format 103  dd/mm/YYYY

whats the query in sql
please you experts!
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Ernesto

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pavel Celba

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Russ Suter

SELECT
  CONVERT(DATETIME, [columnName], 103) as MaxDate
FROM
  [tableName]
WHERE
  [columnName] IN
  (
  SELECT
    MAX([columnName])
  FROM
    [tableName]
  )

Open in new window

Note, if two or more rows in the table share the exact same DATETIME value you will get more than one row using this method.
Pavel Celba

Maybe I am missing something but MAX() from varchar column containing the date in given format does not calculate the maximum date value.
Jim Horn

>I need to get the max date from  a varchar of a table in format 103
(1) Please give us a data mockup of what these varchar's are, as things can get ugly real quick.
(2) Just to state the obvious dates should be stored in date column data types and not as varchar's, as that invites errors to occur when data is entered in a non-date format, and then a later query attempts to convert the value to a date and then fails.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Ernesto

Thank you guys !
regards