Home All Groups Group Topic Archive Search About

Case sensitiveness in doing deletes and updates

Author
26 Jul 2006 1:17 PM
Edward Diener
If I specify a delete of a record in a table, using SQL under Enterprise
Manager or SQL Query Analyzer, based on the string value of a field, and
two records differ only in case regarding that string value, both
records get deleted. Evidently Sql Server is case insensitive in regards
to the delete ( or update ). I do not want that and wish to make Sql
Server case sensitive in this regards. How do I do that ?

Author
26 Jul 2006 1:30 PM
Daniel Crichton
Edward wrote  on Wed, 26 Jul 2006 09:17:27 -0400:

> If I specify a delete of a record in a table, using SQL under Enterprise
> Manager or SQL Query Analyzer, based on the string value of a field, and
> two records differ only in case regarding that string value, both records
> get deleted. Evidently Sql Server is case insensitive in regards to the
> delete ( or update ). I do not want that and wish to make Sql Server case
> sensitive in this regards. How do I do that ?

Case sensitivity is dependent on the collation in use. You should be able to
specify the collation in the DELETE or UPDATE queries, or you could set the
collation on the column itself.

Dan
Author
26 Jul 2006 2:11 PM
Edward Diener
Daniel Crichton wrote:
> Edward wrote  on Wed, 26 Jul 2006 09:17:27 -0400:
>
>> If I specify a delete of a record in a table, using SQL under Enterprise
>> Manager or SQL Query Analyzer, based on the string value of a field, and
>> two records differ only in case regarding that string value, both records
>> get deleted. Evidently Sql Server is case insensitive in regards to the
>> delete ( or update ). I do not want that and wish to make Sql Server case
>> sensitive in this regards. How do I do that ?
>
> Case sensitivity is dependent on the collation in use. You should be able to
> specify the collation in the DELETE or UPDATE queries, or you could set the
> collation on the column itself.

Is there an easy way of just changing the collation on my DELETE or
UPDATE statement to specify case-sensitivity without having to change
anything else ? If so, how would I do it ?

Furthermore I want to specify all data for specific tables of a database
to be case-sensitive. Is there a way to do that other than to have to
change all my SQL statements accordingly.
Author
26 Jul 2006 4:25 PM
Arnie Rowland
Yes, you can change the table definition to a different collation. Check BOL
for ALTER TABLE.

It may be efficient to drop any existing indexes, including clustered
indexes, before changing the collation, and then rebuild the indexes after.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Edward Diener" <ediener@no_spam_incomm.com> wrote in message
news:uN5lw1LsGHA.1192@TK2MSFTNGP04.phx.gbl...
> Daniel Crichton wrote:
>> Edward wrote  on Wed, 26 Jul 2006 09:17:27 -0400:
>>
>>> If I specify a delete of a record in a table, using SQL under Enterprise
>>> Manager or SQL Query Analyzer, based on the string value of a field, and
>>> two records differ only in case regarding that string value, both
>>> records get deleted. Evidently Sql Server is case insensitive in regards
>>> to the delete ( or update ). I do not want that and wish to make Sql
>>> Server case sensitive in this regards. How do I do that ?
>>
>> Case sensitivity is dependent on the collation in use. You should be able
>> to specify the collation in the DELETE or UPDATE queries, or you could
>> set the collation on the column itself.
>
> Is there an easy way of just changing the collation on my DELETE or UPDATE
> statement to specify case-sensitivity without having to change anything
> else ? If so, how would I do it ?
>
> Furthermore I want to specify all data for specific tables of a database
> to be case-sensitive. Is there a way to do that other than to have to
> change all my SQL statements accordingly.
Author
26 Jul 2006 10:21 PM
Erland Sommarskog
Edward Diener (ediener@no_spam_incomm.com) writes:
> Is there an easy way of just changing the collation on my DELETE or
> UPDATE statement to specify case-sensitivity without having to change
> anything else ? If so, how would I do it ?

DELETE tbl WHERE col COLLATE Latin1_General_CS_AS = '<value>'

> Furthermore I want to specify all data for specific tables of a database
> to be case-sensitive. Is there a way to do that other than to have to
> change all my SQL statements accordingly.

   SELECT 'ALTER TABLE ' + o.name + ' ALTER COLUMN ' + c.name + ' ' +
          t.name + '(' + ltrim(str(c.length)) + ') COLLATE ' +
          'Latin1_General_CS_AS ' +
          CASE columnproperty(c.id, c.name, 'AllowsNull')
               WHEN 1 THEN ''
               WHEN 0 THEN 'NOT '
          END + 'NULL'
   FROM   sysobjects o
   JOIN   syscolumns c ON o.id = c.id
   JOIN   systypes t ON t.xtype = c.xtype
                    AND t.xusertype <= 255
   WHERE  t.name LIKE '%char'
     AND  o.xtype = 'U'

Copy and paste the result. When you're done do:

   ALTER DATBASE db COLLATE Latin1_General_CS_AS

In practice it is not that easy. If there are indexes on any column,
they have to be dropped first. One way is to script all indexes from
Enterprise Manager, and then drop all indexes. It gets even more
complicated if a character colunm is in a primary key which is referenced
by a foreign key from somewhere.      

But the essence is that you can determine per column level what collation
to use. A collation both includes language-specific rules, and things
like case-sensitivity. You can also choose when you install SQL Server
to install a case-sensitive collation as the default collation.

Latin1_General_CS_AS is only one of many collations. You may want
to do

   SELECT serverproperty('Collation')

and then use that collation with CI changed to CS.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button