I want to find the most recent date in a database.
I could use sys.dm_db_index_usage_stats to find when it was last updated, but there is no value here if it has not been updated since the last server restart
I could use MAX()
use adventureworks select Max(ModifiedDate) from HumanResources.Department
But I would need to look at every table in the database, find each datetime (or date, smalldatetime, datetime2) field and run the query multiple times, then decide which was most recent.
I want something like
use <some_db> select MAX(<FieldThatIsADate>) from <anytable_in_db>
I have Googled around and am not finding a way to use the datatype in place of a field name. I have not attempted to find a way around naming the specific table.
How can I make this query as portable as realistic?
If I have to build and run a unique query for every table in each database, well that is what I have to do.