Archive for the ‘Functions and DMVs’ Category

The string function SOUNDEX() and its purpose in SQL.

April 25, 2011 Leave a comment

The Soundex code was developed to help negate the effects of all the spelling variations that can occur for similar sounding names ( Smith, Smithe, Smythe, etc.).  This way, users can index records based on a Soundex code based on the sound  and “not” by how it is spelled.

The Soundex system is not infallible – the surnames Gough and Goff sound the same (Goff), but a different code is formed for these two names.  Soundex can however be used to a great extent to get a desired result ( atleast closer to a desired result) This is a commonly used algorithm by many researchers.

In SQL a string function is available for this and is usually comes handy.  The function is

Continue Reading …



Executing the script in batch file using “sqlcmd” command

April 25, 2011 1 comment

 often create batch files to execute sql scripts ( specially SPs) and then schedule it using SQL agent job/Task Scheduler. In this post , I will show you how to create a batch file with sql commands.

First of all, The command that is used to execute SQL queries from commnd prompt is “SQLCMD“. The general syntax of this command is available in the link. I will go with a simple one as below

sqlcmd –E –S Servername –Q Query -o “Output Path”

-E = Trusted connection (windows authentication)
-S = Server Name
-Q = CommandLineQuery
-o = Output file path
a simple command on the commnad prompt can be written as

Reindex the database with high fragmentation using “DBCC DBREINDEX”

October 8, 2010 Leave a comment

DBCC is one of the most commonly used commands by DBAs across the globe. Today I will introduce a simple way to reindex a specific or all indexes in a table using DBCC.

The command used for this is


General Sytax




[ , index_name [ , fillfactor ] ]


The best feature of this command is that,

Continue Reading …



Set Nocount On – Hide the number of records affected by a query

October 7, 2010 Leave a comment

Often it might be necessary to hide the number of records affected by a query. It can be a simple select or insert  statement . Ususally, as soon as we execute a query a message pops up on the messages tab as shown below with the number of records affected.

Continue Reading ….

Script to List Linked Servers

October 7, 2010 Leave a comment

In a large server , it is very common to create high number of linked server connections. it might be required ( not very often) to use this list in the SPs or other objetcs created. Below is a simple query / script that can provide the list of linked servers used.

Continue Reading ..

Get WeekNumber in SQL server using datepart() function

February 11, 2010 8 comments

Many applications and organizations use the week number of a date for some sort of identification, classification, or formatting purpose. Most of the organisation uses week number in their reports related to revenue , refunds etc. SQL server provides an inbuilt function to get the week number of any given date. Remember, the week number that I mention here is the calender week number. The financial week number differs from one institution to another.

The Datepart() function in SQL server can be used for this purpose.

Read More . . . . 

Renaming MS SQL Server database

February 8, 2010 2 comments

Renaming a database is not done very often. However, I have done it myself many times.  this can be done in two ways. The common method used for rename is by using ” Alter Database ” script. An example is as shown below.


The above script will rename the database “OldDbName” with new name “NewDbName”. Another way of acheiving the same is by using the stored Procedure “SP_RENAMEDB” The syntax is as below

EXEC SP_RENAMEDB 'OldDbName' , 'NewDbName'

“Alter” is preferred over the SP_RenameDB as the SP will be deprecated in the future version of  SQL Server.

SQL Server might throw an error while renaming a database as below.

This is due to the existing conections to the database. to overcome this error, need to drop all the connections to the database.  Refer to my previous blog for more info on this.

2. You can set the database to single user mode . refer the link for more info on this.