Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Tuesday, July 3, 2012

SQL Server - Table/Index Exists in Database


//Check If Table Exists in the database
SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].TABLENAME') AND type in (N'U')


//Check if Index Exists in the Database
SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].TABLENAME') AND name = N'INDEXNAME'

Monday, June 18, 2012

SQL Server - Case Sensitive Search

By Default SQL Server search database Irrespective of case of the data in order to make it search with respect to the case of the data we need to set COLLATE identifier to "Latin1_General_CS_AS" of the field you want to make case sensitive like shown below.

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE Latin1_General_CS_AS 
GO 

Thursday, November 17, 2011

SQL - Get All Columns in a Table That Contains NULL

declare @col varchar(255), @cmd varchar(max)

DECLARE getinfo cursor for
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID
WHERE t.Name = 'ADDR_Address'

OPEN getinfo

FETCH NEXT FROM getinfo into @col

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM ADDR_Address WHERE [' + @col + '] IS NOT NULL) BEGIN print ''' + @col + ''' end'
    EXEC(@cmd)

    FETCH NEXT FROM getinfo into @col
END

CLOSE getinfo
DEALLOCATE getinfo
Where 'ADDR_Address' is table name

Tuesday, October 4, 2011

How to use alias in the WHERE or GROUP BY clause

One workaround would be to use a derived table:



select *
from 
   (
   select a + b as aliased_column
   from table
   ) dtwhere dt.aliased_column = something.

Thursday, August 4, 2011

How to Check When Your DB was Last Updated - Sql Server

SELECT OBJECT_NAME(OBJECT_IDAS TableNamelast_user_update,* FROM sys.dm_db_index_usage_statsWHERE database_id DB_ID'Database')