• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2711
  • Last Modified:

SQL Server - get Max Date

I'm using SQL Server and trying to obtain the largest date in a column, yet using this: select max(lastupdatedate) as uDate from... it doesn't seem to get the largest date in the column.  Any ideas why this might be?
0
pegmoran
Asked:
pegmoran
  • 3
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
MAX(lastupdatedate) will get the highest value of the column.
now, if the data type is datetime, for sure.
if the datatype is varchar instead, with a format dd/mm/yyyy or mm/dd/yyyy for example, the ordering will be made in pure ascii code meanings, and without interpreting the string value as datetime.

please specify
0
 
Scott PletcherSenior DBACommented:
Format variations perhaps.  Please try this:

select max(cast(lastupdatedate as date)) as uDate
from...
where isdate(lastupdatedate) = 1
0
 
pegmoranAuthor Commented:
Scott, Get this error:  Type date is not a defined system type.

angelll - yep, it is a varchar field! and won't let me change to datatime
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>angelll - yep, it is a varchar field! and won't let me change to datatime
what is the format?
you should create a new field, copy/convert the date, drop the existing field, create a new field lastupdatedate  with data type datetime, and copy/convert it back to that field.

ALTER TABLE yourtable ADD COLUMN tmp DATETIME NULL

UPDATE yourtable SET tmp = CONVERT(datetime, lastupdatedate  , 101 )

101 -> mm/dd/yyyy
103 -> dd/mm/yyyy
for more formats, check the books online on the cast/convert function

once that worked, the rest should be straightforward..



0
 
pegmoranAuthor Commented:
Conversion failed when converting datetime from character string.
0
 
Scott PletcherSenior DBACommented:
D'OH, sorry.

select max(cast(lastupdatedate as datetime)) as uDate
from...
where isdate(lastupdatedate) = 1

There are no doubt values that are not valid dates in the column; you will have to correct those, at least, prior to changing the column to a datetime data type.  Be aware that you may have to make some changes in your application as well if you change the column type.

This will let you find all the invalid dates so you can correct them:

select lastupdatedate, *
from...
where isdate(lastupdatedate) = 0
0
 
pegmoranAuthor Commented:
Yep, was just replying that I changed it to datetime and it worked!

You guys are fast!!  I appreciate it :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now