• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2700
  • 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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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