Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29843 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > To make SQL Queries Case Sensitive

To make SQL Queries Case Sensitive

Article posted by Guru on 10/7/2008 | Views: 5770 | Category: Sql Server | Level: Beginner red flag


To make SQL Queries Case Sensitive using Collation.
You can make SQL Queries Case sensitive at the column level or Table level or DataBase level

Download


 Download source code for To make SQL Queries Case Sensitive


Introduction

How to make SQL queries case sensitive.

How to do it?

If you installed SQL Server with the default collation options, you might find that the following queries return the same results:

CREATE TABLE mytable

(

mycolumn VARCHAR(10)

)

GO

SET NOCOUNT ON

INSERT mytable VALUES('Case')

GO

SELECT mycolumn FROM mytable WHERE

mycolumn='Case'

SELECT mycolumn FROM mytable WHERE

mycolumn='caSE'

SELECT mycolumn FROM

mytable WHERE mycolumn='case'

You can alter your query by forcing collation at the column level:

SELECT mycolumn FROM mytable WHERE

mycolumn = 'case' AND

CAST(mycolumn AS VARBINARY(10)) = CAST('Case' AS VARBINARY(10))

SELECT mycolumn FROM mytable WHERE

mycolumn = 'case' AND

CAST(mycolumn AS VARBINARY(10)) = CAST('caSE' AS VARBINARY(10))

SELECT mycolumn FROM mytable WHERE

mycolumn = 'case' AND

CAST(mycolumn AS VARBINARY(10)) = CAST('case' AS VARBINARY(10))

-- if myColumn has an index, you will likely benefit by adding

-- AND myColumn = 'case'

 

If you want to do this in a more global way, instead of modifying each individual query, you can force the collation at the database level, or at the column level, using the ALTER DATABASE and ALTER TABLE commands, respectively. You can see the current collation level on the properties tab of the database server, through Enterprise Manager (if you're going to change this setting, MAKE NOTE OF THIS VALUE):

And you can see the description from running the following query: 

 

SELECT DATABASEPROPERTYEX('<databasename>', 'Collation')

As changing this setting can impact applications and SQL queries, I would isolate this test first. In SQL Server 2000, you can easily run an ALTER TABLE statement to change the sort order of a specific column, forcing it to be case sensitive. First, execute the following query to determine what you need to change it back to:

EXEC sp_help
'mytable'

The second recordset should contain the following information, in a default scenario:

 

Column_Name Collation 
----------- ---------------------------------------------- 
mycolumn    SQL_Latin1_General_CP1_CI_AS

Whatever the 'Collation' column returns, you now know what you need to change it back to after you make the following change, which will force case sensitivity:

 

ALTER TABLE mytable

ALTER COLUMN mycolumn

VARCHAR(10)

COLLATE

Latin1_General_CS_AS

GO

SELECT mycolumn FROM mytable WHERE

mycolumn='Case'

SELECT mycolumn FROM mytable WHERE

mycolumn='caSE'

SELECT mycolumn FROM

mytable WHERE mycolumn='case'

If this screws things up, you can change it back, simply by issuing a new ALTER TABLE statement (be sure to replace my COLLATE identifier with the one you found previously): 

 

ALTER TABLE mytable
ALTER COLUMN mycolumn VARCHAR(10) COLLATE SQL_Latin1_General_CP1_CI_AS

Conclusion
This way to make sql querie Case Sensitive

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Guru

About sasi kumar

Experience:0 year(s)
Home page:
Member since:Tuesday, October 07, 2008
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

We have used SUBSTRING in many ways/scenarios. This article explains how it works.

SQL Server 2008 introduces some T-SQL enhancements which is used to improve the database performance and maintenance.

This is part 9 of the series of article on SSIS

The IDENTITY columns are auto incrementing columns provided by SQL Server. There can only be one IDENTITY column per table. SQL Server will take care of incrementing this column automatically.

In this article, I shall show how to concatenate data from COLUMN1 by grouping against COLUMN2 without looping CURSOR.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:56:36 AM