From the category archives:

Developer

SQL Like Statement (or) Condition is basically used for checking if a particular charter string appears in a given string or not. We can use wildcard symbols in the where clause and also in the CASE Statements for pattern matching. Because of this, the SQL Like operator is more powerful than using the equal to (=) or not equal to (! =) symbols for pattern matching and more generically for string comparison. The basic point that we need to keep in mind while using the SQL Like operator is; the result set returned by it is a Boolean value, meaning a TRUE or FALSE.

SQL Like Syntax

The basic syntax for SQL Like is shown below.

match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

SQL Like – Wildcards

The following are the wildcards that can be used with the SQL like condition – %( Percentage symbol), _ (Underscore), [] (Square Parenthesis), ^ (Caret symbol); will show each of them with examples…

Firstly, let us create a temporary table and insert some data into it and see how we can use the SQL Like operator.

CREATE TABLE #SQL_Like (NameID int, ContactName nvarchar(50))

 INSERT INTO #SQL_Like  Values (1,'Amy')
 INSERT INTO #SQL_Like  Values (2,'Anna')
 INSERT INTO #SQL_Like  Values (3,'Brenda')
 INSERT INTO #SQL_Like  Values (4,'Catherine')
 INSERT INTO #SQL_Like  Values (5,'Daniel')
 INSERT INTO #SQL_Like  Values (6,'Emilio')
 INSERT INTO #SQL_Like  Values (7,'Edna')
 INSERT INTO #SQL_Like  Values (8,'Frances')
 INSERT INTO #SQL_Like  Values (9,'Gustavo')
 INSERT INTO #SQL_Like  Values (10,'Hannah')
 INSERT INTO #SQL_Like  Values (11,'Ian')
 INSERT INTO #SQL_Like  Values (12,'John')

Examples for using SQL Like Condition

The most basic form to start with, suppose from the above table if we want to get all the names starting with “A” we can use the below query.

SELECT * FROM #SQL_Like  WHERE ContactName Like 'A%'

The “%” percentage wild card is used for doing a pattern match of any string of zero or more characters. And this is the result that we would get once we are done with executing the above line of T-SQL.

NameID ContactName

1          Amy

2          Anna

If we want to get a list of names that end with “A”, we can use the same wild card, but we will have to put it in the front and the letter that we want to search for at the end, shown like the below.

SELECT * FROM #SQL_Like  WHERE ContactName Like '%A'

And the result for the same would be

NameID ContactName

2          Anna

3          Brenda

7          Edna

Suppose, if there is a requirement where we know that we need to look into the names, in whose name the 2nd letter is “A”, then we have to write a query like the below, the “_” underscore wild card symbol denotes a single character.

SELECT * FROM #SQL_Like  WHERE ContactName Like '_A%'

And the result for the same would be

NameID ContactName

4          Catherine

5          Daniel

10         Hannah

11         Ian

Say, if we want to search for a name, which starts with “A” and in which the second alphabet is something between “A” and “M”, then our result has to be the first name that we had inserted, i.e “Amy”. For this we actually use the “[ ]” square brackets, they are used for searching any single character within the specified range ([a-g]) or set ([abcdefg]).

SELECT * FROM #SQL_Like  WHERE ContactName Like A[A-M]%'

Using SQL NOT Like

Till now we have seen how to directly use the “Like operator”. Now we shall take up an example where we will add “Not” in front of our “Like”. For this, the example that we can take is, say we need a list of all the name’s that do not have the alphabet “A” in them, then our query would resemble the below.

SELECT * FROM #SQL_Like  WHERE ContactName NOT Like '%A%'

Using SQL Like Escape Clause

Everything seems to be fine till now, so, let’s think like this, the wild card symbols are really cool, but what if I need to look into a string for those wild card character’s, puzzled!!!

For this we have these escape clause J. Check the syntax at the beginning of the article
In the below example, my escape character is “!”, so I am actually trying to search for John%, of course practically no names will exist in this way unless there is a type!!! This is just for the sake of taking an example and looking at on how to use it

SELECT * FROM #SQL_Like  WHERE ContactName LIKE '%John!%%' ESCAPE '!'

Let’s take one more example, here we want to look for the underscore symbol, and that’s a wildcard symbol, so decide to make use of the Escape clause over here as well.

SELECT * FROM #SQL_Like  WHERE ContactName LIKE '%John!_%' ESCAPE '!' 

The basic thing over here is that we need to look for a symbol that can be used in the escape clause, means something, that does not exist in our string.

Using SQL Like with Variables and Expressions
We can use the SQL LIKE operator with Variables and Expressions, they can combination of alphabets, alphabets and alpha-numeric, a query with scalar output, a varchar or nvarchar type local variable. But, remember one thing, at the end, everything would be looked at as a string(Not char) by the sql server.

The below is a self-explanatory code snippet. Firstly, we are declaring a variable, then we are setting it to a string “la” and then we are searching for it in the names. In the first, example we are trying to search for characters in the start of the name and in the second example we are trying to search for the characters in the end of the name

DECLARE @Name nvarchar(2)
 SET @Name ='Ia'
 SELECT * FROM #SQL_Like  WHERE ContactName Like @Name +'%'

DECLARE @Name nvarchar(2)
 SET @Name ='N'
 SELECT * FROM #SQL_Like  WHERE ContactName Like '%'+ @Name

Using SQL Like with Case Statement
As stated earlier, the sql like condition returns a true or false, so we can use this in multiple scenarios in conjunction with a CASE statement. Here in the below code snippet we are actually trying to find all the names that start with A, B. We can also construct similar thing where the condition can equal to 0.

SELECT * FROM #SQL_Like
 WHERE CASE
 WHEN ContactName LIKE 'A%' THEN 1
 WHEN ContactName LIKE 'B%' THEN 1
 END = 1

{ Comments on this entry are closed }

People coming from other programming languages to any SQL based language will know how GO TO works in general, even with SQL Server, it is nearly the same.

Go To statement makes the execution of the T-SQL batch to jump and go to a “pre-defined” label. The term pre-defined is important over here, go to statement abruptly makes the code to jump to a label without executing any lines after it, unless the label is next to the Go To line….

We can embed Go to in any of the conditional control-of-flow statements, statement blocks, or procedures but, in practice it is not practical to make much use of this statement as too much of it would not make to code readable and understandable by humans!!! But one thing is, if we have no other choice other than to use this statement then we have to just go ahead using it.

The below is an example from BOL on how to use the GOTO statement, it shows us on how it can be used and how the calls can be made using it, not that the second branch would never be execute because of the condition that is there.

DECLARE @Counter int;

SET @Counter = 1;

WHILE @Counter < 10

BEGIN

SELECT @Counter

SET @Counter = @Counter + 1

IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.

IF @Counter = 5 GOTO Branch_Two  --This will never execute.

END

Branch_One:

SELECT 'Jumping To Branch One.'

GOTO Branch_Three; --This will prevent Branch_Two from executing.

Branch_Two:

SELECT 'Jumping To Branch Two.'

Branch_Three:

SELECT 'Jumping To Branch Three.'

And the below is with a small alter to the above code just showing that we can have a GO TO call/ the Label’s anywhere, there is no precedence.


DECLARE @Counter int;

SET @Counter = 1;

Branch_One:

SELECT 'I am above the GOTO Call'

GOTO Branch_Three; --This will prevent Branch_Two from executing.

WHILE @Counter < 10

BEGIN

SELECT @Counter

SET @Counter = @Counter + 1

IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.

IF @Counter = 5 GOTO Branch_Two  --This will never execute.

END

Branch_Two:

SELECT 'Jumping To Branch Two.'

Branch_Three:

SELECT 'I am below the GOTO Call'

{ Comments on this entry are closed }

This post takes us through on how to query a excel file from SQL Server, doing this using the distributed queries.

Firstly, make sure that the “Run_Value” is set to “1” for “Ad Hoc Distributed Queries”

 

sp_configure 'Ad Hoc Distributed Queries','1'

GO

RECONFIGURE

GO 

 

The below is the result that we are going to get, and we can confirm the change of the configured value by again running sp_configure ‘Ad Hoc Distributed Queries’

 Configuration option ‘Ad Hoc Distributed Queries’ changed from 0 to 1. Run the RECONFIGURE statement to install.

If we do not do this, we will end up getting an error like the below.

Ad hoc access to OLE DB provider ‘Microsoft.Jet.OLEDB.40′ has been denied. You must access this provider through a linked server.””

We can query data by using either of the OPENDATASOURCE or OPENROWSET to query the excel directly.

SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',

'Data Source=C:\test\Excel2Sql.xls;Extended Properties=Excel 8.0')...[Sheet1$]

 SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',

'Excel 8.0;Database C:\test\Excel2Sql.xls', [Sheet2$])

We need to make sure that the source excel file is accessible, means we need to give the correct location, and also we need to make sure that the excel file is not open during this select process, if it is open we would generally get the below error.

OLE DB provider “Microsoft.Jet.OLEDB.4.0″ for linked server “(null)” returned message “Unspecified error“.”

Cannot initialize the data source object of OLE DB provider “Microsoft.Jet.OLEDB.4.0″ for linked server “(null)”.”

Note: – we cannot use this method on a 64 bit OS System, running 64 bit SQL SERVER

{ Comments on this entry are closed }

I have a table where I do logging for all the SQL Server Jobs, I had a requirement where I had to calculate the idle time between two jobs to find the load on the server and better utilise the idle time of the server for other maintenance related operations, for a Job I track many parameters, but the once that are interested for me now for this query are the Job Start Time and the Job End Time, . That is I had to calculate time difference between the last job end time to the current job start time.

 For example, in the below table, I need to know the time difference between the Job End Time for Job ID 1 and Job Start Time of Job ID 2, that’s 120 minutes.

 JobID   JobStartTime                            JobEndTime

1          2011-01-07 02:55:00.000        2011-01-07 03:55:00.000

2          2011-01-07 05:55:00.000        2011-01-07 06:55:00.000

3          2011-01-07 08:55:00.000        2011-01-07 09:55:00.000

 Let me create a temporary table and show how I did it.

CREATE TABLE #JobLog(

JobID int identity(1,1), JobStartTime DATETIME,JobEndTime DATETIME)

INSERT INTO #JobLog(JobStartTime,JobEndTime)

VALUES('2011-01-07 02:55:00.000','2011-01-07 03:55:00.000')

INSERT INTO #JobLog(JobStartTime,JobEndTime)

VALUES('2011-01-07 05:55:00.000','2011-01-07 06:55:00.000')

INSERT INTO #JobLog(JobStartTime,JobEndTime)

VALUES('2011-01-07 08:55:00.000','2011-01-07 09:55:00.000')

 

Checking for table and it’s records.

SELECT * FROM #JobLog

Time difference between Last Job End Time and Current Job Start Time

WITH CTE as(

SELECT JobID, JobStartTime, JobEndTime, (

    SELECT MAX(T1.JobEndTime)

    FROM #JobLog T1

    WHERE T1.JobEndTime < T2.JobStartTime

  ) AS Previous_JobEndTime

  FROM #JobLog T2

 )

SELECT JobID, JobStartTime, JobEndTime

      , DATEDIFF(MI,Previous_JobEndTime,JobStartTime) AS IdleTime

FROM CTE

 

 And the below is the resuly.

JobID   JobStartTime                            JobEndTime                             IdleTime

1          2011-01-07 02:55:00.000        2011-01-07 03:55:00.000        NULL

2          2011-01-07 05:55:00.000        2011-01-07 06:55:00.000        120

3          2011-01-07 08:55:00.000        2011-01-07 09:55:00.000        120

dropping the temporary table

DROP TABLE #JobLog

{ Comments on this entry are closed }

 Primarily there are two ways to execute a string or a variable in a string format as sql, they are using EXEC and SP_EXECUTESQL.

 sp_executesql is always to be preferred for executing Dynamic SQL because, sp_executesql supports parameter substitution and also there are more chances of the reuse of execution plan when we use sp_executesql.
 
What we should remember when using sp_ExecuteSQL is that, it always executes as a separate batch irrespective if the batch/ procedure it is contained in.The transact SQL Contained in a variable which is being executed through the SP_ExecuteSQL has its Execution plan generated separately.
 
Here are a set of example on how a sp_Executesql can be used, and what are the basic mistakes that we commit while using sp_Executesql.
 

Using sp_ExecuteSQL, sp_executesql error – Data Type of Vairable

 
Here is a basic example of sp_ExecuteSQL, where we declare a variable and assign a valid sql to it and when executed it throws an error “Procedure expects parameter ‘@statement’ of type ‘ntext/nchar/nvarchar.”
 
This tells us that, it is compulsory to use variable of the above data types.
Use AdventureWorks
GO
DECLARE @SqlStr varchar(500)
SET @SqlStr='SELECT TOP 10 EmployeeID,NationalIDNumber,Title FROM HumanResources.Employee'
EXEC sp_executesql @SqlStr
Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

Using sp_ExecuteSQL, sp_executesql error - Parsed and Compiled

 
Now, lets make another mistake deliberately to see what happens if we use an invalid sql statement in the variable which is being executed through sp_ExecuteSQL, it tells us that the sql query in the variable is parsed and compiled only when executed through the sp_ExcuteSQL
Use AdventureWorks
GO
DECLARE @SqlStr nvarchar(500)
SET @SqlStr='SELECT TOP 10 EmployeeID,A_NationalIDNumber,Title FROM HumanResources.Employee'
EXEC sp_executesql @SqlStr
Msg 207, Level 16, State 1, Line 1
Invalid column name 'A_NationalIDNumber'.
 
 

Using sp_ExecuteSQL,Correct Method

 
Now, lets go and put correct sql in the variable and run it using sp_ExecuteSQL to see how it works. 
Use AdventureWorks
GO
DECLARE @SqlStr nvarchar(500)
SET @SqlStr='SELECT TOP 10 EmployeeID,NationalIDNumber,Title FROM HumanResources.Employee'
EXEC sp_executesql @SqlStr

Using sp_ExecuteSQL, Parameter Concatenation

 
The below code snippet shows how a string can be formed dynamically and then be assigned to a variable which is in turn executed through the sp_ExecuteSQL stored procedure
Use AdventureWorks
GO
DECLARE @Employeeid int
DECLARE @SqlStr nvarchar(500)
SET @EmployeeID=10
SET @SqlStr='SELECT TOP 10 EmployeeID,NationalIDNumber,Title FROM HumanResources.Employee WHERE EmployeeID= ' + Cast(@Employeeid as Varchar)
EXEC sp_executesql @SqlStr

Using sp_ExecuteSQL, How to Save the result set in a table

 
This example, shows how a result set that is being returned via sp_ExecuteSQL be stored in a temporary table.
USE AdventureWorks
GO
CREATE TABLE #usp_TestInsertWithSp_exec(EmployeeID int, NationalIDNumber nvarchar(15),Title nvarchar(50))
DECLARE @Employeeid int
DECLARE @SqlStr nvarchar(500)
SET @EmployeeID=10
SET @SqlStr='SELECT TOP 10 EmployeeID,NationalIDNumber,Title FROM HumanResources.Employee WHERE EmployeeID= ' + Cast(@Employeeid as Varchar)
INSERT INTO #usp_TestInsertWithSp_exec
EXEC sp_executesql @SqlStr
SELECT * FROM #usp_TestInsertWithSp_exec
DROP TABLE #usp_TestInsertWithSp_exec

Using sp_ExecuteSQL, OUTPUT Parameters

 
The below code gives a basic example of How to specify output parameters when we use the sp_executesql stored procedure in SQL Server. 
Use AdventureWorks
GO
DECLARE @Employeeid int
DECLARE @NationalID nvarchar(15)
DECLARE @SqlStr nvarchar(500)
SET @EmployeeID=10
SET @SqlStr='SELECT @NationalID= NationalIDNumber FROM HumanResources.Employee WHERE EmployeeID= ' + Cast(@Employeeid as Varchar)+''
EXEC sp_executesql @SqlStr, N'@NationalID nvarchar(15) OUTPUT',@NationalID OUTPUT
PRINT @NationalID
I suppose, that I have covered most of the points on SP_ExecuteSQL, if you have any comments regarding this sp_ExecuteSQL topic or any other topic on SQL Server, Please feel free to comment.
If you have liked the article and want to get the articles (or) new posts at sql like, subscribe to articles through email by entering your email address in the top right and verifying the same.

{ Comments on this entry are closed }

What is the difference between union and union all in sql server?

The UNION operator joins result sets from tow or more SELECT statements. The basic Syntax of using “UNION” operator is

SELECT Column List FROM Table1

UNION [ALL]

SELECT Column List FROM Table2 
  • ALL Clause in the query is optional

A couple of things that need to be kept in mind while using the “SQL UNION” operator are

  1. The tables should have same data types for the columns
  2. The Order in which we get the columns should be same
  3. If we want to enforce Sort order for the final result set, we should have a “Order By” Clause on the last “SQL SELECT Statement” that we are going to have in our query.

One main difference between using the “ UNION ALL” clause and not using it is, when we use the “UNION” without the “ALL” Clause we get distinct records in the overall result set, but when we use the “UNION ALL” we get the complete set of results with out the removal of any duplicates.

So, based on this we can say that, UNION for SQL Server is just concatenation whereas UNION ALL has one more operation of identifying and removing the duplicates.

Now, let’s take an example to see what is happening???

Let’s go ahead and create two tables for our learning purpose, Table1 with a single int column and also table2 with single int column and insert some records with duplicates in them

Table 1

CREATE TABLE Table1(Col1 int)

INSERT INTO Table1 Values (1)

INSERT INTO Table1 Values (1)

INSERT INTO Table1 Values (2)

INSERT INTO Table1 Values (3)

INSERT INTO Table1 Values (3)

INSERT INTO Table1 Values (4)

Table 2

CREATE TABLE Table2(Col1 int)

 INSERT INTO Table2 Values (4)

INSERT INTO Table2 Values (5)

INSERT INTO Table2 Values (5)

INSERT INTO Table2 Values (6)

INSERT INTO Table2 Values (7)

INSERT INTO Table2 Values (8)

So, now let’s go and write the UNION queries on these tables and see what the result set is and also what is the Execution Plan for these queries to get better understanding of these operators.

SELECT Col1 FROM Table1

UNION

SELECT Col1 FROM Table2

SQL UNION

SELECT Col1 FROM Table1

UNION ALL

SELECT Col1 FROM Table2

SQL UNION ALL

If you look at the operator that is used to unite the two sets for a UNION ALL, you’ll see it’s a Concatenation operator, combining two sets of data without any further thought of whether rows have previously been seen.

For a UNION, You can see a Distinct Sort as it makes sure it doesn’t return the same row twice.

So, now coming to the point on which one is better, from the above we can say that if duplicates are not an issue then definitely we should go for UNION ALL, but we need to really look at what our query needs to return and make sure users queries are answered correctly.

So, I think here I have pointed out nearly all the difference between union and union all in sql server.

If you have liked the article and want to get the articles (or) new posts at sql like, subscribe to articles through email by entering your email address in the top right and verifying the same. If you have any comments, then the below form is for you, go ahead!!!

{ Comments on this entry are closed }

How do we concatenate numbers in sql?

Let us take an example for this, say; we create a table with two integer columns.

USE TempDB

GO

CREATE TABLE concatenate_integers ( col1 int, col2 int)

GO

Now let us put a couple of records over here on which we can run our analysis on.

INSERT INTO concatenate_integers

Values(1,2)

INSERT INTO concatenate_integers

Values(11,12)

INSERT INTO concatenate_integers

Values(21,31)

GO

For concatenation in SQL Server, we use the “+” operator.

So, as a test let’s see what’s going to happen if we are going to write a query

select Col1,Col2,Col1+Col2 as Conc from concatenate_integers

Col1       Col2       Conc

1              2              3

11           12           23

21           31           52

Oh, we got a sum!!!

So, for integer type columns the “+” operator returns sum.

But that’s not what we are looking for what we want as result is 12, 1112, 2131…

So, first we need to cast these columns as strings before we concatenate the numbers.

Here it goes…

select Col1,Col2,Cast(Col1 as varchar)+Cast(Col2 as varchar) as Conc from concatenate_integers

Col1       Col2       Conc

1              2              12

11           12           1112

21           31           2131

Learning: The + sign sums integer type columns but concatenates character type columns.

You can also try the same with the below code.

DECLARE @A INT

DECLARE @B INT

DECLARE @C VARCHAR

DECLARE @D VARCHAR

SET @A=1

SET @B=2

SET @C = 'A'

SET @D = 'B'

SELECT @A+@B

-- RESULT IS 3

SELECT @C+@D

-- RESULT IS AB

SELECT CAST(@A AS VARCHAR) +CAST(@B AS VARCHAR)

-- RESULT IS 12

{ Comments on this entry are closed }

Incremental Loads using SQL Queries

Problem: Using SSIS we have very easy ways to load a SQL Server Data Mart Incrementally, but how to perform Incremental Data Load when we do not have SSIS with us, in other words using T-SQL Queries.

Solution: To begin with let us have a look at the architecture for loading the data mart, we can get our data from one or more data sources into a staging area. This step should purely depend upon the business and technical needs.
Whenever we want to load data incrementally, we should make sure that we are able to find a unique identifier to distinguish one record from the other. We need to take a look at the source timestamp, primary key, identity key, and unique key columns of the rows that we capture to ensure there is a continuous representation of each unique record in the dimension or a fact table.
 
Loading Fact Tables
In most cases we would be able to find a unique time stamp column or a unique key which is Bill No, Invoice No or a Incident No. Generally here we can go for Delete and Insert method between Stage and Mart Levels, for this we shall first identify the rate of Insertion and Updation of data in the source, and decide the number of days records that we need to get from the source to stage
 
Loading Dimension Tables
Loading dimension would be the trickiest part as we have to make sure all the changes are processed up to the data marts. Suppose, for example if we take a customer dimension, we can have a new customer being added, customers details being updated and in some rare cases customers information being deleted when there is no relevant fact data.
 
The above scenario can be tackled easily depending upon the amount of data by
Should import all the records or last “X” days records depending upon the need and
1.       Doing complete truncate and Insert. à If we want to have History information which is currently not available in transaction tables this method should not be used.
 
2.       Using Except Keyword:  If we have large number of duplicates and handle them on the fly we can go with the Except keyword, using it between the stage and mart, this would give us all the records that are not there in Mart and it would automatically remove duplicates.
 
Insert into Mart_Table
SELECT * FROM Stage_Table
 EXCEPT
SELECT * FROM Mart_Table;
 
 
3.       Doing a delete and Insert à we should delete the records that are available in the stage from mart and Insert all the records from stage to mart. Doing this we will also be having records that are not available in stage and this will also take care of any updating being data at the source level.
 
The above described were a few methods that I have used for loading data incrementally without using SSIS.
Conclusion: we can perform incremental loads using T-SQL when we do not have an access to SSIS.

{ Comments on this entry are closed }

BASICS OF WORKING WITH SQL SERVER CHECKSUM

This Article talks about what SQL Server CHECKSUM and  SQL Server BINARY CHECKSUM are and how they can be used effectively in SQL Server.

In simple terms, we can describe CHECKSUM as “a calculation performed on a sequence of characters or on a string to yield a single integer value, which will differ for every different sequence“. This would greatly help us in our day to day activities of comparing data in different systems or different set of tables, and also to retrieve data more quickly for a matched or unmatched set of records.

With respect to SQL Server, CHECKSUM would return integer value computed over a row in a table, or for a particular cell, or for an expression, or over a list of expressions. Or in other words CHECKSUM computes a hash value, called the checksum, over its list of arguments. Below is an example where we are taking a string as an argument for the checksum function.

For Example,

select CHECKSUM('abcdef') as ChecksumValue 

ChecksumValue

140941491

Example: 2

Create a temporary Table

Insert data for test

  CREATE TABLE #ChecksumTest( col1 INT, col2 VARCHAR(10))

INSERT INTO #ChecksumTest VALUES(1,'abc')

INSERT INTO #ChecksumTest VALUES (2,'bcd')

– Check the check sum value

 SELECT CHECKSUM(*) AS ChecksumValue FROM #ChecksumTest

ChecksumValue

34416

34481

We can make use of check sum for equality searches. We have to keep one thing in mind over here while using this; the check sum value that we get is purely dependent on the order of columns mentioned. The point which is also to be noted over here is, If we are comparing two columns or two expressions, the NULL values of the same data type are considered as same!!!

Now, taking an example for the NULL value comparison, let us take the above temp table and insert two records with NULL values and then see what would be the results, frankly, this makes life easier as we have a good standard method to compare NULL’s!!!
For Example

We can compare NULLS Also

insert into #ChecksumTest values(3,NULL)

select CHECKSUM(col2)as ChecksumValue from #ChecksumTest where col1 = 3

And the results for the above would be

Checksum Value

2147483647

 insert into #ChecksumTest values(4,NULL)

select CHECKSUM(col2)as ChecksumValue from #ChecksumTest where col1 = 4

And the results for the above would be

ChecksumValue

2147483647

Extreme Fast Indexes with CHECKSUM

One of the major uses of check sum is for building Hash Indexes. Actually, the checksum computes a hash value depending upon the parameters/arguments we pass (In the example 2, we passed “*” all columns). If we build columns in a table purely with these hash values and if we index these columns, they are called Hash Indexes.

Hash Indexes can be effectively used for equality searches, so for example, whenever we have strings with length of more than 50 and we have to equality search on those columns (generally for speeding up queries we build Indexes, but Indexes are not recommended for length of >50), then we can build these Hash columns and can perform search based on this column, as Indexes on Integer columns are more effective than Indexes on character/ string columns.

Now, let us try the above by trying it out with an example, continuing with the above temporary table created…

For Example

ALTER TABLE #ChecksumTest

ADD cs_col2 AS CHECKSUM(col2);

GO

CREATE INDEX NIX_Col2 ON #ChecksumTest (col2);

GO

BINARY CHECKSUM

It returns a binary checksum value computed over a row of a table or over a list of expressions. This is generally used to check if the data in a particular row of a table has changed.

For example, continuing with the above created temporary table, run the script below and observe the difference before updating the records and after updating the records

SELECT BINARY_CHECKSUM(*)as BinaryChecksum from #ChecksumTest

where col1=1;

GO

UPDATE #ChecksumTest set col2 = 'TEST'where col1 = '1';

GO

SELECT BINARY_CHECKSUM(*)as BinaryChecksum from #ChecksumTest

where col1=1;

GO

Binary Checksum

455504

Binary Checksum

5935073

CHECKSUM and BINARY_CHECKSUM are similar functions, but, return different values for the string and character data types. SQL Server settings can cause strings with different representation to compare equal.

Where we cannot Use Checksum

While using, one important point that has to be kept in mind would be that it is not compatible with text, ntext, image, cursor, xml. When used, the system may either ignore these columns or throws an error if these are the only columns given as arguments.

{ Comments on this entry are closed }