Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

A quick way to find recently entered datetime entries in tables

By Gaby Abed,

Every once in a while, you find yourself with a database you have no idea who owns it or when it was last accessed. In addition to running a trace on it, you can run this script to get a quick snapshot of any datetime column entries. What it does is use syscolumns to extract each datetime column that exists, map it to it's approrpriate table, and wrap it in a dynamic SQL statement as SELECT TOP 1 <datetime column> FROM <target table> SORT BY <datetime column> DESC'


It's quick and dirty but gets the job done. It does rely on one MASSIVE assumption however. There has to be a datetime column that gets updated in someway (update or insert). There are some apps that use varchar(8) columns for example to capture dates such as '20100502' and this script will not catch those unfortunately. Also, if the column is not indexed and you have a very large number of rows, this could also be problematic due to the descending sort.


To help resolve this, I've defaulted the commands to print out the statements in case you need to verify against some of the larger tables if there is an index on that column, and cut/paste the results to query analayzer.


[EDIT: Fixed buggy code, included a where clause in the select from #findaccesstimes loop]

Total article views: 1822 | Views in the last 30 days: 4
Related Articles

DateTime Problem



datetime column conversion

trying to update datetime column


Help with datetime column

Help with datetime column


How To Alter a datetime column to Numeric

How To Alter a datetime column to Numeric


BCP Error with datetime columns

BCP using -n switch from one database flat file is causing me issues with datetime columns


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones