Thursday, October 15, 2009

Year to date for a particular date

select max(dailydate)dailydate from tbldate where
month(dailydate)<=month('10/09/09') and day(dailydate)<=day('10/09/09')
and year(dailydate)<=year('10/09/05')
group by year(dailydate)
order by dailydate desc

Monday, September 14, 2009

alter primary column datatype sqlserver

ALTER TABLE [dbo].[Users]
DROP CONSTRAINT PK_Users
GO

ALTER TABLE [dbo].[Users]
ALTER COLUMN [UserID] INT NOT NULL
GO

ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users] PRIMARY KEY ( [UserID] )
GO

Friday, August 14, 2009

A macro to add, delete, and find hidden and non-hidden bookmarks in Word

Method 1: Code to Strip All Bookmarks from a Document
Sub StripAllBookmarks()
Dim stBookmark As Bookmark
ActiveDocument.Bookmarks.ShowHidden = True
If ActiveDocument.Bookmarks.Count >= 1 Then
For Each stBookmark In ActiveDocument.Bookmarks
stBookmark.Delete
Next stBookmark
End If
End Sub


Method 2: Code to Strip Only Hidden Bookmarks from a Document

Sub StripHiddenBookmarks()
Dim stBookmark As Bookmark
ActiveDocument.Bookmarks.ShowHidden = True
If ActiveDocument.Bookmarks.Count >= 1 Then
For Each stBookmark In ActiveDocument.Bookmarks
If Left(stBookmark, 1) = "_" Then
stBookmark.Delete
End If
Next stBookmark
End If
End Sub



Method 3: Code to Add a Hidden Bookmark
The following code adds a bookmark named "_HiddenBookmark1" to a document at the location of the insertion point or selection.

Sub AddHiddenBookmark
ActiveDocument.Bookmarks.Add Name:="_HiddenBookmark1"
End Sub



Sunday, August 2, 2009

Find all tables that contain a certain column

SELECT c.TABLE_NAME,TABLE_TYPE,COLUMN_NAME,ORDINAL_POSITION,IS_NULLABLE,
DATA_TYPE,NUMERIC_PRECISION FROM INFORMATION_SCHEMA.COLUMNS c
JOIN INFORMATION_SCHEMA.TABLES t ON c.TABLE_NAME = t.TABLE_NAME WHERE COLUMN_NAME ='columnname' ORDER BY TABLE_TYPE ,c.TABLE_NAME

Friday, July 3, 2009

Search String in Stored Procedure

SQL Server 2000
USE AdventureWorks
GO
--Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%Employee%'
GO
--Option 2
SELECT DISTINCT o.name ,o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%Employee%'
GO

SQL Server 2005
USE AdventureWorks
GO
--Searching for Empoloyee table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'
GO
--Searching for Empoloyee table and RateChangeDate column together
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'
AND OBJECT_DEFINITION(OBJECT_ID) LIKE '%RateChangeDate%'