Memo for SQL Server queries

This article is a short memo for some queries that I use for SQL Server and might get some updates sometimes.

Connection String for SQL Server 2000

ConnectionString = “Data Source=<SQL Server IP>;Database=<NAME OF YOUR DB>;User Id=<SQL USER>;Password=<YourPassword>;”

Find tables name from a specific field

Select table_name,column_name from information_schema.columns where column_name=’EmployeeID’

Display Primary key for each tables

Select i1.table_name,i2.colomn_name from information_schema.table_constraints i1
inner join information_schema_colomn_usage i2 on i1.constraint_name = i2.constraint_name
where i1.constraint_type=’PRIMARY KEY’

Search for column and return all table where EmployeeID is

Select table_name,column_name from information_schema.columns where column_name=’EmployeeID’

Search for column and return all views where Estimat is

Select i1.table_name from information_schema.table_constraints i1
where i1.table_name not like ‘__-%’

Or try the one below

Select table_name from information_schema.views
where table_name like ‘%Estimat%’

Convert Today’s date into int

Convert(Varchar(35),GetDate(),112)

Convert an int into a DateTime

Convert(DATETIME, LEFT(Date, 8))

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *