Monday, August 08, 2005

Calculating Age

To display the correct age of a person:

declare
@date as DATETIME
SET @date = '09/17/1976'

SELECT CAST( datediff(d, @date, getdate()) / 364 as VARCHAR) + ' Years ' +
CAST( datediff(m, dateadd(year, datediff(d, @date, getdate()) / 364, getdate()), getdate()) as VARCHAR) + ' Months ' +
CAST( datepart(d, getdate()) as VARCHAR) + ' Days' AS Age

Thursday, August 04, 2005

List Tables in a database

The following query lists all the tables that are available in a current database

SELECT db_name() [Database], name [Table Name],
CASE xtype
WHEN 'S' THEN 'System Table'
WHEN 'V' THEN 'View'
ELSE 'User Table' END [Type]
FROM sysobjects
WHERE xtype IN ('S','V','U')
ORDER BY db_name(), xtype, name