Want to know where dates are used in your database ?
This script
1)
Displays all tables containing columns with a datatype of datetime
or
smalldatetime
2) Displays all tables containing columns with a column name
containing (Year, month day…)
3) Shows stored procedures containing
Year, month day or any of the
date functions (datediff,dateadd ….)
4)
It can be modified to search for your own date user types
SET NOCOUNT
ON
declare @msg varchar(255)
Select @msg=’Days until Jan 1st 2000 = ‘
+
convert(varchar(5),Datediff(dd,getdate(),’1 jan 2000′))
print
@msg
select @msg=replicate(‘=’,datalength(@msg))
print @msg
print ‘
‘
drop table #t1
go
create table #t1 (search_string
varchar(255))
Insert into #t1 values (‘%[ ]date%’)
Insert into #t1 values
(‘%day%’)
Insert into #t1 values (‘%Month%’)
Insert into #t1 values
(‘%Year%’)
Insert into #t1 values (‘%Week%’)
Insert into #t1 values
(‘%dateadd%’)
Insert into #t1 values (‘%datediff%’)
Insert into #t1 values
(‘%datepart%’)
Insert into #t1 values (‘%getdate()%’)
— Show tables
with columns or names containing date references
select distinct
‘Table’=o.name , ‘Column’= c.name , ‘Format’=
t.name
from sysobjects o ,
syscolumns c , systypes t ,#t1 m
Where o.id=c.id
and o.type=’U’
and
c.type=t.type
and (t.name like ‘%date%’
or patindex(search_string,c.name)
> 0)
order by o.name,c.name
print ‘ ‘
— Show stored
procedures containing date related words
select
search_string=substring(search_string,1,20),
‘SP
Name’=upper(o.name),
Seq=colid ,
‘SP
Line’=substring(text,patindex(search_string,text)-5, 30)
from syscomments c ,
sysobjects o , #t1 t
where o.id=c.id
and patindex(search_string,text) > 0
order by substring(search_string,1,20),name
go