Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search a database for ALL invalid date records. 1

Status
Not open for further replies.

Jamfool

IS-IT--Management
Apr 10, 2003
484
GB
I was wondering is there anyway to return a columns type? e.g date,int,double etc etc.?

The reason I ask is because I need to search a database for date values that are below , and above a certain value or simply invalid. If it were only a few tables it wouldnt be a problem, but this database consists of over a hundred tables.
I would like to search thru the database listing the table and column which are set as a type date.

Then using the tablename&column do a check on them to see which records contain invalid data.

Problem:
1.First list all the tables
2.Get the columns & coresponding types
3. Select all the date columns and do a validity check
 
According to the ANSI standard you can retrieve the data type for a column by using the following select statement

Code:
select * from information_schema.columns
 where data_type = 'DATE'
 
Thanks again swamp boogie :) your a star

Heres the code produced. if any one else needs to search database wide. Just use it to auto generate the search SQL
then run them.

Code:
	select 	'(select * from ' + 
		table_name + 
		' where ' + 
		column_name + 
		' <1/1/1900) print ''' + 
		table_name + 
		'/' + 
		column_name + 
		'''' + 
		char(10) 
		
	from 	information_schema.columns
	where	data_type in ('datetime')
	and	table_schema = 'dbo'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top