SOUNDEX – Discovering The Sounds Of Words In SQL Server

Facebooktwitterredditpinterestlinkedinmail

Let’s face it, not everyone is a perfect speller all the time. Exact match queries are pretty easy to do in SQL Server. But, what if you wanted to correct their spelling or search based on a misspelled word? How is this possible? In SQL Server 2008+ you have the ability to do this with the SOUNDEX function.

SOUNDEX
The SOUNDEX function will take an input word and convert it in to a 4 character code representing the sound of the word. For example: goat -> G300.

So what does this 4 character code mean? The first character represents the first character in the string, the rest of the characters represent the rest of the characters in the string. Of course SQL Server does some magic to come up with this code. If you are interested more in the makeup of this code, see the MSDN link at the bottom of this post. Essentially, this 4 character code represents the sound of the word.

 
How To Use It
You can use this just like any other function in SQL Server.

SELECT	SOUNDEX('dog') AS SoundOfDog

An easy way to search based on word sound would be something like this:

SELECT	*
FROM	Animal
WHERE	SOUNDEX(AnimalName) = SOUNDEX('dug')

The above query would find words like “dog”, “dig”, “dag”

 
Reference: http://msdn.microsoft.com/en-us/library/ms187384.aspx

How To Create A Date Table

Facebooktwitterredditpinterestlinkedinmail

Many times when developing SQL Server databases you have the requirement to show all dates in a range. One way to do this is with a date table. One of the most common reasons that I use a date table is when I have a reconciliation report. In a reconciliation report I want to show all of the days in a date range, even if they don’t have data on those days. SQL Server doesn’t have a function to generate dates in a range. So to accomplish this, I create a date table using the following script.

CREATE
TABLE	DateTable
		(DateValue	DATETIME)

GO

DECLARE @StartDate		DATETIME = '01/01/2014'
DECLARE @NumberOfDays	INT = 3000

INSERT
INTO	DateTable
		(DateValue)
SELECT	DATEADD(DAY, RowNum, @StartDate) AS DateValue
FROM	(SELECT	TOP (@NumberOfDays)
				p1.PARAMETER_ID,
				ROW_NUMBER() OVER (ORDER BY p1.PARAMETER_ID) - 1 AS RowNum
		 FROM	sys.all_parameters p1
		 CROSS	JOIN sys.all_parameters p2) NumberTable

 

What Is SQL Server

Facebooktwitterredditpinterestlinkedinmail

What Is SQL Server
SQL Server is a database management system (DBMS) produced by Microsoft. The purpose of SQL Server is to store and retrieve data for use in computer applications. These applications could be mobile apps, web sites, desktop applications, or more.

Data in SQL Server is stored in tables. These tables have rows and columns, much like you would see in an Excel spreadsheet. SQL Server accesses this data using a coding language called T-SQL (Transact-SQL). This code is called a query. Here is a sample query that pulls all of the data from a table.

SELECT	*
FROM	AnimalTable

 
Easiest Definition Of “What Is SQL Server”
SQL Server is a program built by Microsoft that other programs can use to store and retrieve data.

SQL Server Synonyms – What They Are And How To Use Them

Facebooktwitterredditpinterestlinkedinmail

What Is A Synonym?

A synonym in SQL Server can be thought of like having another name for a table.  The table doesn’t even have to be in your own database.  The intent of having synonyms is for an easy way to reference objects in another database.  It basically gives you a short alias to call server objects that live in another database.  You can create a synonym for tables, views, stored procedures, and functions!

 
Example Without Synonym:

SELECT	*
FROM	FarmAnimals.dbo.SoundsOfAnimals

 
Example With Synonym:

SELECT	*
FROM	AnimalSounds

In the example above, the synonym AnimalSounds represents FarmAnimals.dbo.SoundsOfAnimals.

 
NOTE: Because the synonym is basically an alias for a different server object, INSERT, UPDATE, SELECT, DELETE operations happen directly on the base table and not on the synonym.

 
Why Would You Use A Synonym?

There are 2 main reasons for using synonyms.  The first reason is to make the code a little easier to read.  It makes all of the remote objects look like local objects.  The second reason is that it creates a layer of abstraction.  If the base table changes, then you only need to change the synonym.  If you did not use a synonym, then you would need to go in to every stored procedure, view, function, etc… and change the reference to the new table.

 
Real-Life Use

So… although it is a neat concept, I really don’t use synonyms very much.  The one application where I see it very useful is when you do not own the database that you are referencing (like if you bought a 3rd party product).  Product updates could change the table/stored procedure names… if you use a synonym to reference the database tables, then it would only take 1 change on your side to get your program working again.

 
 
Reference:  http://msdn.microsoft.com/en-us/library/ms187552.aspx

 

How To Use The OUTPUT Clause

Facebooktwitterredditpinterestlinkedinmail

The OUTPUT clause is an amazing part of SQL Server that many people do not know about.  As corny as it is, I still remember the day that I found it.  I had been searching for the whole day trying to figure out how to get the identity values from a large amount of data that I just inserted in to the database.  There had to be a way… right?  After searching and searching, I gave up and just accepted that it is not possible… a few days later I found exactly what I was looking for… the OUTPUT clause.

 
The OUTPUT clause is a part of the query that will return data from before or after the operations is completed.  Let’s say that you inserted data in to a table and you wanted the ID column values (which are auto-numbers).  The OUTPUT clause gives you this information!  It can work on INSERT, DELETE, UPDATE, and MERGE statements.  I will take you through examples of each.

 
To access the data that is being altered in your sql statement, you need to use special column prefixes that SQL Server makes available to you.  The two special prefixes are “inserted” and “deleted”.  During an insert statement, the inserted prefix is available for you to use.  During a delete statement, the deleted prefix is available to you.  During the update and merge statements, both the deleted and inserted prefixes are available to you.  In these cases, the deleted represents the data before it was changed and the inserted represents the data after it was changed.

 
One thing to note is that the data being outputted must go in to a table or table variable.

 
OUTPUT Clause On An INSERT Statement

DECLARE	@OutputData	TABLE
		(AnimalID	INT,
		 AnimalName	VARCHAR(50))

-- Insert into the table and stick the resulting animal
--   name and identity column in to a temp variable
INSERT
INTO	Animal
		(AnimalName)
OUTPUT	inserted.AnimalID, inserted.AnimalName
INTO	@OutputData
		(AnimalID, AnimalName)
VALUES	('Pig'),
		('Dog'),
		('Chinchilla')

-- View the inserted data
SELECT	*
FROM	@OutputData

 
OUTPUT Clause On A DELETE Statement

DECLARE	@OutputData	TABLE
		(AnimalID	INT,
		 AnimalName	VARCHAR(50))

DELETE
FROM	Animal
OUTPUT	deleted.AnimalID, deleted.AnimalName
INTO	@OutputData
		(AnimalID, AnimalName)
WHERE	AnimalName = 'Pig'

-- View the deleted data
SELECT	*
FROM	@OutputData

 
OUTPUT Clause On An UPDATE Statement

DECLARE	@OutputData	TABLE
		(AnimalID	INT,
		 OldAnimalName	VARCHAR(50) ,
		 NewAnimalName	VARCHAR(50))

-- Update the table and output the animal name
--   and identity column to the temp table
UPDATE	Animal
SET	AnimalName = 'Swine'
OUTPUT	inserted.AnimalID, deleted.AnimalName, inserted.AnimalName
INTO	@OutputData
		(AnimalID, OldAnimalName, NewAnimalName)
WHERE	AnimalName = 'Pig'

-- View the updated data
SELECT	*
FROM	@OutputData

 
OUTPUT Clause On A MERGE Statement

DECLARE	@OutputData	TABLE
		(AnimalID	INT,
		 OldAnimalName	VARCHAR(50),
		 NewAnimalName	VARCHAR(50))

DECLARE	@ChangeData	TABLE
		(OldAnimalName	VARCHAR(50),
		 NewAnimalName	VARCHAR(50))

INSERT
INTO	@ChangeData
VALUES	('Pig', 'Swine')

-- Update the table and output the animal name
--   and identity column to the temp table
MERGE	Animal AS target
USING	@ChangeData AS source
		ON (source.OldAnimalName = target.AnimalName)
WHEN	MATCHED
THEN	UPDATE
		SET target.AnimalName = source.NewAnimalName
OUTPUT	inserted.AnimalID, deleted.AnimalName, inserted.AnimalName
INTO	@OutputData
		(AnimalID, OldAnimalName, NewAnimalName);

-- View the updated data
SELECT	*
FROM	@OutputData

 
 
Reference: http://msdn.microsoft.com/en-us/library/ms177564.aspx